Create an array with objects in Javascript

I have defined the object, and the array in the scope.

//Define o array que deve ser preenchido com os objetos
var itensList = [];

//Define os argumentos do objeto
var oItem = {
    Codig: 0,
    Desc: "",
    Unid: "KG",
    Quant: 0,
    Custo: 0,
    IL: "",
    Centro: "",
    Saldo: o
}

Then, for each row of the Dhtmlx Grid, include the results in the object...

        //Para cada linha do grid, busca os resultados
        mygrid.forEachRow(function (id) {
            //Se o Codigo do item for maior que zero..
            if (mygrid.cells(id, 0).getValue() != 0) {
                // .. adiciona os valores ao objeto
                oItem.Codig = mygrid.cells(id, 0).getValue();
                oItem.Desc = mygrid.cells(id, 1).getValue();
                oItem.Unid = mygrid.cells(id, 2).getValue();
                oItem.Quant = mygrid.cells(id, 3).getValue();
                oItem.Custo = mygrid.cells(id, 4).getValue();
                oItem.IL = mygrid.cells(id, 5).getValue();
                oItem.Centro = mygrid.cells(id, 6).getValue();
                oItem.Saldo = mygrid.cells(id, 7).getValue();
                // Adiciona o objeto a lista
                itensList.push(oItem);
            }
        });

At the end of this program, if I have two rows filled in the Grid, it will have an array, with two objects, each with the parameters listed above, but, the two stay with the same values, the values of the last row swept by the code, that is, two equal objects, when the rows are different. Debugging, vi that by adding the object to the array with itensList.push(oItem);, it makes the object that is already there have the same values.

How to make each object have the values of each row?

Author: bfavaretto, 2014-04-09

3 answers

This happens because vc is inserting the same object into the vector. By changing the value of the field, it changes the references in the vector.

One way to solve this would be to clone the object .

Another, simpler way would be to change the variable declaration into if.

mygrid.forEachRow(function (id) {
  //Se o Codigo do item for maior que zero..
  if (mygrid.cells(id, 0).getValue() != 0) {
    var oItem = {};

    // .. adiciona os valores ao objeto
    oItem.Codig = mygrid.cells(id, 0).getValue();
    oItem.Desc = mygrid.cells(id, 1).getValue();
    oItem.Unid = mygrid.cells(id, 2).getValue();
    oItem.Quant = mygrid.cells(id, 3).getValue();
    oItem.Custo = mygrid.cells(id, 4).getValue();
    oItem.IL = mygrid.cells(id, 5).getValue();
    oItem.Centro = mygrid.cells(id, 6).getValue();
    oItem.Saldo = mygrid.cells(id, 7).getValue();

    // Adiciona o objeto a lista
    itensList.push(oItem);
  }
});
 3
Author: Beterraba, 2017-05-23 12:37:28

The problem is that you are setting the same "instance" of the item object, and it will be modified in all its references, since it is being declared in a global scope in javascript.

In Javascript you don't need to declare the structure of your object, since objects in javascript are dynamic, you can do just that:

//Para cada linha do grid, busca os resultados
mygrid.forEachRow(function(id) {
    //Se o Codigo do item for maior que zero..
    if (mygrid.cells(id, 0).getValue() != 0) {
        // .. adiciona os valores ao objeto
        var oItem = new Object(); // cria um objeto novo (uma nova instância)
        oItem.Codig = mygrid.cells(id, 0).getValue();
        oItem.Desc = mygrid.cells(id, 1).getValue();
        oItem.Unid = mygrid.cells(id, 2).getValue();
        oItem.Quant = mygrid.cells(id, 3).getValue();
        oItem.Custo = mygrid.cells(id, 4).getValue();
        oItem.IL = mygrid.cells(id, 5).getValue();
        oItem.Centro = mygrid.cells(id, 6).getValue();
        oItem.Saldo = mygrid.cells(id, 7).getValue();
        // Adiciona o objeto a lista
        itensList.push(oItem);
    }
});

And remove the object statement:

//Define os argumentos do objeto
var oItem = {
    Codig: 0,
    Desc: "",
    Unid: "KG",
    Quant: 0,
    Custo: 0,
    IL: "",
    Centro: "",
    Saldo: o
}
 2
Author: Fernando Leal, 2014-04-09 13:46:31

You need to clear the variable oItem, keep creating it out of looping and try this:

 //Para cada linha do grid, busca os resultados
        mygrid.forEachRow(function (id) {
            //Se o Codigo do item for maior que zero..
            if (mygrid.cells(id, 0).getValue() != 0) {
                // .. adiciona os valores ao objeto

                oItem = {}; // AQUI eu inicializo a variável

                oItem.Codig = mygrid.cells(id, 0).getValue();
                oItem.Desc = mygrid.cells(id, 1).getValue();
                oItem.Unid = mygrid.cells(id, 2).getValue();
                oItem.Quant = mygrid.cells(id, 3).getValue();
                oItem.Custo = mygrid.cells(id, 4).getValue();
                oItem.IL = mygrid.cells(id, 5).getValue();
                oItem.Centro = mygrid.cells(id, 6).getValue();
                oItem.Saldo = mygrid.cells(id, 7).getValue();
                // Adiciona o objeto a lista
                itensList.push(oItem);
            }
        });

Or not to lose the initially set properties:

 //Para cada linha do grid, busca os resultados
        mygrid.forEachRow(function (id) {
            //Se o Codigo do item for maior que zero..
            if (mygrid.cells(id, 0).getValue() != 0) {
                // .. adiciona os valores ao objeto

                var oItem = { // AQUI eu inicializo a variável
                  Codig: 0,
                  Desc: "",
                  Unid: "KG",
                  Quant: 0,
                  Custo: 0,
                  IL: "",
                  Centro: "",
                  Saldo: o
                } 

                oItem.Codig = mygrid.cells(id, 0).getValue();
                oItem.Desc = mygrid.cells(id, 1).getValue();
                oItem.Unid = mygrid.cells(id, 2).getValue();
                oItem.Quant = mygrid.cells(id, 3).getValue();
                oItem.Custo = mygrid.cells(id, 4).getValue();
                oItem.IL = mygrid.cells(id, 5).getValue();
                oItem.Centro = mygrid.cells(id, 6).getValue();
                oItem.Saldo = mygrid.cells(id, 7).getValue();
                // Adiciona o objeto a lista
                itensList.push(oItem);
            }
        });
 2
Author: Alessandro Gomes, 2014-04-09 13:49:11