Search code examples
javascriptjsonapinestedjavascript-objects

How do I access nested JSON objects that are variable from item to item


I am trying to pull out "point_spread_away" from the lines object...the affiliate id sub objects (1, 3, 4, 6, 7, etc.) are not consistent and vary from item to item (sometimes "1" does not exist, in the pictured example "2" does not exist, etc). If I wanted to access the "point_spread_away" from the first affiliate id object in the lines object, how would I do that?

enter image description here


Solution

  • If I wanted to access the "point_spread_away" from the first affiliate id object in the lines object, how would I do that?

    Object.values(lines).map(({ spread: { point_spread_away: _ } }) => _)[0]
    

    This will give you the value of the point_spread_away key of the first object. Actually without the [0] it will give you an array of all of them.