John Mark
John Mark

Reputation: 119

javascript add object dynamically

I have this code where I added two Reviews (with the ID and NAME):

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

        }
    };

enter image description here How can I add them dynamically (i will get the reviews data from other sites using 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?

Upvotes: 0

Views: 60

Answers (5)

Akash
Akash

Reputation: 4553

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

}

Upvotes: 1

th3n3wguy
th3n3wguy

Reputation: 3737

In your example output, the Reviews property of the object is an array of objects. 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"
  });
}

Upvotes: 0

cjatstackoverflow
cjatstackoverflow

Reputation: 117

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

Upvotes: 0

Armin Bu
Armin Bu

Reputation: 1360

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

Upvotes: 0

Anthony L
Anthony L

Reputation: 2169

This should get you moving in the correct direction:

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

Upvotes: 0

Related Questions