附加到不是数组的JS对象?

时间:2022-07-25 21:15:49

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.

通过$ .getJSON调用将其读入我的javascript。

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

所以我有JS对象“回复”,它包含所有这些数据。

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?

有人可以提供替代方法吗?

6 个解决方案

#1


3  

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

回复['entries']。push({“Bar”:0})不起作用,因为条目不是Array类型,只是一个普通的Object。

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

使用回复['entries'] [“Bar”]或reply.entries.Bar。见下面的演示:

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

#2


5  

With ES2017 you could use:

使用ES2017,您可以使用:

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

#3


4  

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

还有一个,因为为什么不〜遇见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);

#4


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

或回复[“条目”] [“Foo”] = 0

or reply.entries["Foo"] = 0

或reply.entries [“Foo”] = 0

#5


1  

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

为了将新条目插入JSON对象,您可以尝试这样的方法:

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

object [“someproperty”] = somevalue;或object.someproperty = somevalue;

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

假设您已经在变量some​​property和somevalue中获得了键和值,您可以简单地将它们插入:

reply.entries[someproperty] = somevalue

#6


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;

#1


3  

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

回复['entries']。push({“Bar”:0})不起作用,因为条目不是Array类型,只是一个普通的Object。

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

使用回复['entries'] [“Bar”]或reply.entries.Bar。见下面的演示:

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

#2


5  

With ES2017 you could use:

使用ES2017,您可以使用:

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

#3


4  

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

还有一个,因为为什么不〜遇见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);

#4


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

或回复[“条目”] [“Foo”] = 0

or reply.entries["Foo"] = 0

或reply.entries [“Foo”] = 0

#5


1  

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

为了将新条目插入JSON对象,您可以尝试这样的方法:

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

object [“someproperty”] = somevalue;或object.someproperty = somevalue;

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

假设您已经在变量some​​property和somevalue中获得了键和值,您可以简单地将它们插入:

reply.entries[someproperty] = somevalue

#6


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;