简体   繁体   English

javascript动态添加对象

[英]javascript add object dynamically

I have this code where I added two Reviews (with the ID and NAME): 我在以下代码中添加了两个评论(带有ID和NAME):

var data =  {
        SKU: 'CS4',
        Name: 'Dell Laptop Inspiron 41',
        Quantity: 1,
        ItemPrice: 121,
        "Reviews" : [
        {
            "ID": 2551,
            "Name": 'john',
        },
        {
            "ID": 255551,
            "Name": 'j5ohn',

        }
    };

在此处输入图片说明 How can I add them dynamically (i will get the reviews data from other sites using ajax) ? 如何动态添加它们(我将使用ajax从其他站点获取评论数据)?

I tried these code but I don't know why its not working and I am totally lost. 我尝试了这些代码,但我不知道为什么它不起作用,我完全迷路了。

var data =  [{
        SKU: 'CS4',
        Name: 'Dell Laptop Inspiron 41',
        Quantity: 1,
        ItemPrice: 121,

    }];

var total_reviews = 5;

for (i=1; i<=total_reviews; i++) {

     Object.assign(data.Reviews, { "ID": data.from.other.sites.ID, "Name": data.from.other.sites.NAME });

}

Can you please give me advise which part I am missing ? 您能告诉我我缺少哪一部分吗? or can you give me hints or codes for me to start? 还是可以给我提示或代码让我开始?

data[0].Reviews = []

for (i=1; i<=total_reviews; i++) {

     data[0].Reviews.push({ "ID": data.from.other.sites.ID, "Name": data.from.other.sites.NAME })

}

In your example output, the Reviews property of the object is an array of objects. 在示例输出中,对象的Reviews属性是对象数组。 This simple code should help you do that: 这个简单的代码应该可以帮助您做到这一点:

 var data = [ { SKU: 'CS4', Name: 'Dell Laptop Inspiron 41', Quantity: 1, ItemPrice: 121 } ]; var total_reviews = 5; for (var i = 1; i <= total_reviews; i++) { // This doesn't work because you are trying to push it to an Array. // Object.assign(data.Reviews, { "ID": data.from.other.sites.ID, "Name": data.from.other.sites.NAME }); // First make sure that the array is initialized. data[0].Reviews = data[0].Reviews || []; // Then add the object to your array. data[0].Reviews.push({ "ID": "data.from.other.sites.ID", "NAME": "data.from.other.sites.NAME" }); } 

This should get you moving in the correct direction: 这应该使您朝正确的方向前进:

if(!data.Reviews){
  data.Reviews = []
}
review = {"ID": "someId", "Name": "someName"}
data.Reviews.push(review)

it seems like your data object isn't an plain object. 看来您的数据对象不是普通对象。 It's an array. 这是一个数组。

So if you want to add new Reviews into an element of that array, find the element by it's key (unique identifier) and push the new Review into the elements 'Reviews' property. 因此,如果要将新的评论添加到该数组的元素中,请按其键(唯一标识符)找到该元素,然后将新的评论推入元素的“评论”属性中。

const index = data.indexOf(element => element.id === id);
if (index !== -1) {
    for (let i = 0; i < total_reviews; i++) {
        data[index]['Reviews'].push({ "ID": data.from.other.sites.ID, "Name": data.from.other.sites.NAME };
    }
}

Please check this: 请检查以下内容:

 var data = { SKU: 'CS4', Name: 'Dell Laptop Inspiron 41', Quantity: 1, ItemPrice: 121, "Reviews" : [ { "ID": 2551, "Name": 'john', }, { "ID": 255551, "Name": 'j5ohn', }] }; var total_reviews = 5; for (i=1; i<=total_reviews; i++) { data.Reviews.push({ "ID": 'id_'+i, "Name": 'name_'+i }) } console.log(data.Reviews); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM