Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.push replaces all my array elements with the last one inserted

I have the following code:

var ship_array = new Array();

var ship_object = new Object();
    ship_object.builder_id = 0;
    ship_object.list_id = 0;
    ship_object.ship_id = 0;
    ship_object.title_id = 0;

Then in a save function, I do:

function saveAll() {
    // Array cleaning
    while (ship_array.length) { ship_array.pop(); }

    // Cyclic save function
    $.each($(".ship-block"), function () {
        ship_object.builder_id = parseInt($(this).attr("data-counter"));
        ship_object.list_id = list.id;
        ship_object.ship_id = parseInt($(this).attr("data-ship-id"));
        ship_array.push(ship_object);
    });
    console.log(ship_array);
}

While debugging with Chrome, every ship_object has the correct values in every cycle, but when I print the array, every object has the same values, which are all corresponding to the last one inserted. Literally have no clue why it is happening. Ideas?

like image 240
nevetz1911 Avatar asked Oct 22 '15 13:10

nevetz1911


People also ask

Does array push change the original array?

Examples of push in JavaScript and common errors Reassigning the array with the output from push is a common error. To avoid this error you need to remember that push changes the array, and returns the new length. If you reassign the variable with the return value from push() you are overwriting the array value.

What happens when you push an array into an array?

The push() method adds one or more elements to the end of an array and returns the new length of the array.

Does array push overwrite?

The Array. push() method has to be passed a String value, else it will override all values in the array to the last value pushed.

Which method is used to push the element in an array at last?

The push() method is used for adding an element to the end of an array.


2 Answers

Basically you are using the reference of the object and updating the same object again and again. Try creating a new object every time when the loop iterates,

function saveAll() {
    //while (ship_array.length) { ship_array.pop(); }
    ship_array = [];
    $(".ship-block").each(function () {
        ship_object = {}; // creating a new object here!!
        ship_object.builder_id = parseInt($(this).attr("data-counter"));
        ship_object.list_id = list.id;
        ship_object.ship_id = parseInt($(this).attr("data-ship-id"));
        ship_array.push(ship_object);
    });
    console.log(ship_array);
}
like image 60
Rajaprabhu Aravindasamy Avatar answered Sep 22 '22 07:09

Rajaprabhu Aravindasamy


In your code, your array contains references to the same object, you only update properties of that object.

You should create a new object for each array element. Note that you can that using an object literal syntax like so:

function saveAll() {
    // Array cleaning
    ship_array = [];

    // Cyclic save function
    $.each($(".ship-block"), function () {
        var ship_object = {
            builder_id: parseInt($(this).attr("data-counter")),
            list_id: list.id,
            ship_id: parseInt($(this).attr("data-ship-id"))
        };
        ship_array.push(ship_object);
    });
    console.log(ship_array);
}

Note that I also reinitialized the array with a new empty array ([]) rather than popping from it in a loop.

like image 23
kralyk Avatar answered Sep 24 '22 07:09

kralyk