Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending to a JS object that is not an array?

I have an object that looks something like this

{
  "_id": "DEADBEEF",
  "_rev": "2-FEEDME",
  "name": "Jimmy Strawson",
  "link": "placeholder.txt",
  "entries": {
    "Foo": 0
  }
}

Which is read into my javascript with a $.getJSON call.

So I have the JS object "reply" that holds all this data.

I need to append items such that "entries" becomes extended as follows:

{
  "_id": "DEADBEEF",
  "_rev": "2-FEEDME",
  "name": "Jimmy Strawson",
  "link": "placeholder.txt",
  "entries": {
    "Foo": 0,
    "Bar": 30,
    "Baz": 4
  }
}

I have tried

reply['entries'].push({"Bar": 0});

But that does not work (I presume because nothing is an array)

Can someone provide an alternative method?

like image 248
Hubert S. Cumberdale Avatar asked Oct 13 '16 04:10

Hubert S. Cumberdale


People also ask

Can you append to an object JavaScript?

To append to an object with JavaScript, we can use the spread operator. let alerts = { 1: { app: "helloworld", message: "message" }, 2: { app: "helloagain", message: "another message" }, }; alerts = { ... alerts, alertNo: 2, }; to spread the existing properties of alerts into a new object with ... .

How do you add an item to an object?

Object. assign({},objectname,{property: value}); In the example, we have used the assign () method to add a new element to an existing object. For that, we used an empty target place and are creating a new object so it does not change the existing object values.

How do you push values in an object?

In order to push an array into the object in JavaScript, we need to utilize the push() function. With the help of Array push function this task is so much easy to achieve. push() function: The array push() function adds one or more values to the end of the array and returns the new length.


3 Answers

With ES2017 you could use:

const input = (
  { _id: 'DEADBEEF'
  , _rev: '2-FEEDME'
  , name: 'Jimmy Strawson'
  , link: 'placeholder.txt'
  , entries: (
      { Foo: 0
      }
    )
  }
)
 
const output = (
  { ...input
  , entries: (
      { ...input.entries
      , Bar: 30
      , Baz: 4
      }
    )
  }
)

console.info(JSON.stringify(output, null, 2))

Note: this will not mutate the original input object, but instead return a new one (typically a good thing).

like image 101
cchamberlain Avatar answered Oct 19 '22 15:10

cchamberlain


Here's one more because why not ~ meet Object.assign

let reply = {"_id":"DEADBEEF","_rev":"2-FEEDME","name":"Jimmy Strawson","link":"placeholder.txt","entries":{"Foo":0}};

Object.assign(reply.entries, {
    Bar: 30,
    Baz: 4,
    'Bar Baz': 0
});

console.log('reply =', reply);
like image 4
Phil Avatar answered Oct 19 '22 17:10

Phil


reply['entries'].push({"Bar": 0}) does not work as entries is not of type Array but just a plain Object.

Use reply['entries']["Bar"] or reply.entries.Bar. See demo below:

var reply = {
  "_id": "DEADBEEF",
  "_rev": "2-FEEDME",
  "name": "Jimmy Strawson",
  "link": "placeholder.txt",
  "entries": {
    "Foo": 0,
    "Bar": 30,
    "Baz": 4
  }
}
reply['entries']["Bar"] = 0;

console.log(reply);
like image 3
kukkuz Avatar answered Oct 19 '22 17:10

kukkuz