将 JavaScript 对象添加到 JavaScript 对象

发布于 2024-08-11 16:45:05 字数 955 浏览 3 评论 0原文

我希望在另一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

物价感观 2024-08-18 16:45:05
var jsonIssues = []; // new Array
jsonIssues.push( { ID:1, "Name":"whatever" } );
// "push" some more here
var jsonIssues = []; // new Array
jsonIssues.push( { ID:1, "Name":"whatever" } );
// "push" some more here
草莓酥 2024-08-18 16:45:05

由于我的第一个对象是本机 JavaScript 对象(像对象列表一样使用),因此 push 在我的场景中不起作用,但我通过添加新密钥解决了这个问题,如下所示:

MyObjList['newKey'] = obj;

除此之外,了解如何删除之前插入的相同对象可能很有用:

delete MyObjList['newKey'][id];

希望它能帮助别人,就像它帮助我一样。

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:

MyObjList['newKey'] = obj;

In addition to this, may be useful to know how to delete the same object as inserted before:

delete MyObjList['newKey'][id];

Hope it helps someone as it helped me.

一杆小烟枪 2024-08-18 16:45:05
var jsonIssues = [
 {ID:'1',Name:'Some name',Notes:'NOTES'},
 {ID:'2',Name:'Some name 2',Notes:'NOTES 2'}
];

如果你想添加到数组中,那么你可以这样做

jsonIssues[jsonIssues.length] = {ID:'3',Name:'Some name 3',Notes:'NOTES 3'};

或者你可以使用其他人发布的推送技术,这也很好。

var jsonIssues = [
 {ID:'1',Name:'Some name',Notes:'NOTES'},
 {ID:'2',Name:'Some name 2',Notes:'NOTES 2'}
];

If you want to add to the array then you can do this

jsonIssues[jsonIssues.length] = {ID:'3',Name:'Some name 3',Notes:'NOTES 3'};

Or you can use the push technique that the other guy posted, which is also good.

沫尐诺 2024-08-18 16:45:05

// 递归地将 object2 合并到 object1

$.extend( true, object1, object2 );

// 将 object2 合并到 object1

$.extend( object1, object2 );

https://api.jquery.com/ jquery.extend/

// Merge object2 into object1, recursively

$.extend( true, object1, object2 );

// Merge object2 into object1

$.extend( object1, object2 );

https://api.jquery.com/jquery.extend/

思念绕指尖 2024-08-18 16:45:05

如果它不是对象数组,您可以执行以下操作:

let student= {
  name : 'Mr. Anderson',
  id: 35
}

student['grade'] = 10; //for a property. 

结果:

student= {
  name : 'Mr. Anderson',
  id: 35,
  grade:10
}

您还可以添加对象:

let student= {
 personalData:{
  //personal data key-value
 }
}

let academicData = {
 //academic data key-value
}

student['academicData'] = academicData;

结果:

 student{
        personalData{},
        academicData{}
    }

If it's not an array of object you can do this:

let student= {
  name : 'Mr. Anderson',
  id: 35
}

student['grade'] = 10; //for a property. 

Result:

student= {
  name : 'Mr. Anderson',
  id: 35,
  grade:10
}

You also can add an object:

let student= {
 personalData:{
  //personal data key-value
 }
}

let academicData = {
 //academic data key-value
}

student['academicData'] = academicData;

Result:

 student{
        personalData{},
        academicData{}
    }
眼眸印温柔 2024-08-18 16:45:05
jsonIssues = [...jsonIssues,{ID:'3',Name:'name 3',Notes:'NOTES 3'}]
jsonIssues = [...jsonIssues,{ID:'3',Name:'name 3',Notes:'NOTES 3'}]
爱你是孤单的心事 2024-08-18 16:45:05

如果第一个 obj 中有属性,并且必须向其中添加 obj,则 Object.assign() 将删除它。

为了避免这种损失,我在下面编写了一个函数。请注意,它通过引用复制嵌套对象。

首先,您应该将要添加到您的 obj 中的所有 obj 添加到 arr 中。您可以通过 arr.push() 轻松完成。然后只需使用 Fn。

function addMyObjs (objInit, arrWithObjs) {
    let map = new Map();
    for (let [key, value] of Object.entries(objInit)) {
        map.set(key, value);
    }
    arrWithObjs.forEach((item) => {
        for (let [key, value] of Object.entries(item)) {
            map.set(key, value);
        }
    });
    return Object.fromEntries(map);
}

let objSecond = {id: 2,};

let obj = {
    name: "Tolya",
    age: 33,
    sex: "man",
};

let obj3 = {"fruits": {"apples": true, "plums": false,}};

let arr = [obj, obj3];

objSecond = addMyObjs(objSecond, arr);

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.

function addMyObjs (objInit, arrWithObjs) {
    let map = new Map();
    for (let [key, value] of Object.entries(objInit)) {
        map.set(key, value);
    }
    arrWithObjs.forEach((item) => {
        for (let [key, value] of Object.entries(item)) {
            map.set(key, value);
        }
    });
    return Object.fromEntries(map);
}

let objSecond = {id: 2,};

let obj = {
    name: "Tolya",
    age: 33,
    sex: "man",
};

let obj3 = {"fruits": {"apples": true, "plums": false,}};

let arr = [obj, obj3];

objSecond = addMyObjs(objSecond, arr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文