admin管理员组

文章数量:1312912

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" bees 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?

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" bees 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?

Share Improve this question edited Sep 5, 2017 at 1:50 kukkuz 42.4k6 gold badges64 silver badges102 bronze badges asked Oct 13, 2016 at 4:16 Hubert S. CumberdaleHubert S. Cumberdale 1319 bronze badges 13
  • 3 reply.entries.Bar = 0; PS: reply is a JS object not a JSON. – zerkms Commented Oct 13, 2016 at 4:18
  • JSON object can't look like that. It's an intrinsic object in JavaScript engines. – Teemu Commented Oct 13, 2016 at 4:20
  • Thank you @zerkms!! Now if "Bar" has to be a string, like "Bar Baz" what should I do? Sorry. I am new to javascript! – Hubert S. Cumberdale Commented Oct 13, 2016 at 4:21
  • 2 Let's not bang on too long with all this JSON / object literal debate. We all know what OP means. OP, please read this ~ benalman./news/2010/03/theres-no-such-thing-as-a-json – Phil Commented Oct 13, 2016 at 4:22
  • 1 I think OP means reply.entries['Bar Baz'] = 0 – Phil Commented Oct 13, 2016 at 4:22
 |  Show 8 more ments

6 Answers 6

Reset to default 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).

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

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

it bees 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

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

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;

本文标签: javascriptAppending to a JS object that is not an arrayStack Overflow