将 JavaScript 对象添加到 JavaScript 对象
我希望在另一个 JavaScript 对象中包含 JavaScript 对象:
Issues:
- {"ID" : "1", "Name" : "Missing Documentation", "Notes" : "Issue1 Notes"}
- {"ID" : "2", "Name" : "Software Bug", "Notes" : "Issue2 Notes, blah, blah"}
- {"ID" : "2", "Name" : "System Not Ready", "Notes" : "Issue3 Notes, etc"}
// etc...
因此,我希望“Issues”来保存每个 JavaScript 对象,这样我就可以直接说 Issues[0].Name 或 Issues[2] .ID 等。
我已经创建了外部问题 JavaScript 对象:
var jsonIssues = {};
我需要向其中添加一个 JavaScript 对象,但不知道如何添加。我想说:
Issues<code here>.Name = "Missing Documentation";
Issues<code here>.ID = "1";
Issues<code here>.Notes = "Notes, notes notes";
有什么办法可以做到这一点吗?谢谢。
更新:根据给出的答案,声明一个数组,并根据需要推送 JavaScript 对象:
var jsonArray_Issues = new Array();
jsonArray_Issues.push( { "ID" : id, "Name" : name, "Notes" : notes } );
感谢您的回复。
I'd like to have JavaScript objects within another JavaScript object as such:
Issues:
- {"ID" : "1", "Name" : "Missing Documentation", "Notes" : "Issue1 Notes"}
- {"ID" : "2", "Name" : "Software Bug", "Notes" : "Issue2 Notes, blah, blah"}
- {"ID" : "2", "Name" : "System Not Ready", "Notes" : "Issue3 Notes, etc"}
// etc...
So, I'd like "Issues" to hold each of these JavaScript objects, so that I can just say Issues[0].Name, or Issues[2].ID, etc.
I've created the outer Issues JavaScript object:
var jsonIssues = {};
I'm to the point where I need to add a JavaScript object to it, but don't know how. I'd like to be able to say:
Issues<code here>.Name = "Missing Documentation";
Issues<code here>.ID = "1";
Issues<code here>.Notes = "Notes, notes notes";
Is there any way to do this? Thanks.
UPDATE: Per answers given, declared an array, and am pushing JavaScript objects on as needed:
var jsonArray_Issues = new Array();
jsonArray_Issues.push( { "ID" : id, "Name" : name, "Notes" : notes } );
Thanks for the responses.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于我的第一个对象是本机 JavaScript 对象(像对象列表一样使用),因此
push
在我的场景中不起作用,但我通过添加新密钥解决了这个问题,如下所示:除此之外,了解如何删除之前插入的相同对象可能很有用:
希望它能帮助别人,就像它帮助我一样。
As my first object is a native JavaScript object (used like a list of objects),
push
didn't work in my scenario, but I resolved it by adding new key as follows:In addition to this, may be useful to know how to delete the same object as inserted before:
Hope it helps someone as it helped me.
如果你想添加到数组中,那么你可以这样做
或者你可以使用其他人发布的推送技术,这也很好。
If you want to add to the array then you can do this
Or you can use the push technique that the other guy posted, which is also good.
// 递归地将 object2 合并到 object1
// 将 object2 合并到 object1
https://api.jquery.com/ jquery.extend/
// Merge object2 into object1, recursively
// Merge object2 into object1
https://api.jquery.com/jquery.extend/
如果它不是对象数组,您可以执行以下操作:
结果:
您还可以添加对象:
结果:
If it's not an array of object you can do this:
Result:
You also can add an object:
Result:
如果第一个 obj 中有属性,并且必须向其中添加 obj,则 Object.assign() 将删除它。
为了避免这种损失,我在下面编写了一个函数。请注意,它通过引用复制嵌套对象。
首先,您应该将要添加到您的 obj 中的所有 obj 添加到 arr 中。您可以通过 arr.push() 轻松完成。然后只需使用 Fn。
If you have properties in first obj and you have to add your objs to it, then Object.assign() will erase it.
To avoid this loss I've written a function below. Be aware, it copies nested objs by refference.
First you should add all objs you want to add to your's obj to an arr. You can do it easily by arr.push(). Then just use the Fn.