5

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?

kukkuz
  • 41,512
  • 6
  • 59
  • 95

6 Answers6

5

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).

Community
  • 1
  • 1
cchamberlain
  • 17,444
  • 7
  • 59
  • 72
  • 1
    That's a great point regarding immutability of the original data. I'd make that text larger :) – Phil Oct 13 '16 at 04:51
4

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);
Phil
  • 157,677
  • 23
  • 242
  • 245
  • @DivyanshuMaithani it's probably the `let`. What browser are you running? – Phil Oct 13 '16 at 04:36
  • Yeah, must be it. My Chrome is a bit outdated. Maybe using strict mode will do the trick. – Divyanshu Maithani Oct 13 '16 at 04:37
  • @DivyanshuMaithani no, it won't. `let` is available from Chrome 41.0 which is over a year old now. In any case, this is just an example to run here on StackOverflow – Phil Oct 13 '16 at 04:42
3

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);
kukkuz
  • 41,512
  • 6
  • 59
  • 95
  • 1
    thank you sir! will accept in 8 minutes when it lets me – Hubert S. Cumberdale Oct 13 '16 at 04:23
  • 1
    There is no Array [*type*](http://ecma-international.org/ecma-262/7.0/index.html#sec-ecmascript-data-types-and-values). There is a built–in Array constructor from which array instances can be created. The constructor and its instances are type [*Object*](http://ecma-international.org/ecma-262/7.0/index.html#sec-object-type). ;-) – RobG Oct 13 '16 at 04:29
  • Exactly! An `Array` is a special `Object`- that's what I tried to convey :) – kukkuz Oct 13 '16 at 04:31
2

it becomes an object, so just add what you want :-

reply.entries.Foo = 0
reply.entries.Bar = 30
reply.entries.Baz = 4

or reply["entries"]["Foo"] = 0

or reply.entries["Foo"] = 0

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
1

In order to insert new entries into the JSON object, you could try something like this:

object["someproperty"] = somevalue; or object.someproperty = somevalue;

Say you've got the key and values in variables someproperty and somevalue, you could simply insert them as:

reply.entries[someproperty] = somevalue
Divyanshu Maithani
  • 13,908
  • 2
  • 36
  • 47
1

Here is the alternate method:

reply = {
          "_id": "DEADBEEF",
          "_rev": "2-FEEDME",
          "name": "Jimmy Strawson",
          "link": "placeholder.txt",
          "entries": {
            "Foo": 0
          }
        }
reply.entries.Bar=30;
reply.entries.Baz=4;
reply["entries"]["Bar"]=30;
reply["entries"]["Baz"]=4;
Kruti Patel
  • 1,422
  • 2
  • 23
  • 36
Jyforzs
  • 11
  • 1