Taylor Liss
Taylor Liss

Reputation: 593

How do I add a value to an object in an array that is the value of a key in another object?

I have an object named session. It looks like this:

{ 
  "cookie":   
              {
                  "expires": null
              },
  "toDo": 
          [
              {
                  "name": Taylor,
                  "id": 0,
                  "location": boston
              }
          ]
}

I want to add another key-value pair for the temperature to the group under "toDo".

I've tried session.toDo.push({'temp; 65}); but I end up with this:

{ 
  "cookie":   
              {
                 "expires": null
              },
  "toDo": 
         [
              {
                  "name": Taylor,
                  "id": 0,
                  "location": boston
              },
              {
                  "temp": 65
              }
          ]
}

What I want is this:

{ 
  "cookie":   
              {
                 "expires": null
              },
  "toDo": 
         [
              {
                  "name": Taylor,
                  "id": 0,
                  "location": boston,
                  "temp": 65
              }
          ]
}

Upvotes: 0

Views: 27

Answers (1)

Jack
Jack

Reputation: 21163

You want the first item in the array:

session.toDo[0].temp = 65

Upvotes: 2

Related Questions