Skip to content Skip to sidebar Skip to footer

Javascript: Push Array Of Objects To Array

I have an array of objects (global scope): objects =[{id:'0',radius:3,},{id:'1',radius:2,}]. This array is being manipulated by changing the single objects: objects =[{id:'0',radi

Solution 1:

You can use JSON.stringify() to push the current array as a string to an array.

Solution 2:

You are adding the same reference into your history, you need to first clone it and then add it in, like so:

JSON.parse(JSON.stringify(arr));

Solution 3:

This should solve the issue:

functionsave_to_history(){
    history_obj.push(JSON.parse(JSON.stringify(objects)))
}

as you need to deepcopy the object else it'll keep on changing, whenever your objects changes and consequently all the values will be same, which exactly is what you're getting.

Post a Comment for "Javascript: Push Array Of Objects To Array"