Skip to content
Advertisement

Push data into existing array using javascript

I am trying to find a way to build my custom array, but I am not able to do so.

What I have done so far:

  1. I have my array constructed like so:

const super_array = [];

super_array.push({
 produs: rezultate[i].produs,
 email: rezultate[i].email,
 pretDorit: rezultate[i].pretDorit
});
  1. Further down into the code, I want to assign new data to the array, like so :

     for(let j=0; j<results.length; j++) {
                 priceNumber = parseFloat(results[j].replace('<span>Lei</span>', '')) ;
                 super_array.push({price: priceNumber})
             }
    

    Result:

Right now, I get the following structure:

super_array({
      produs: rezultate[i].produs,
      email: rezultate[i].email,
      pretDorit: rezultate[i].pretDorit
    }, {pret: priceNumber});

What I would like to get is:

super_array({
      produs: rezultate[i].produs,
      email: rezultate[i].email,
      pretDorit: rezultate[i].pretDorit,
      pret: priceNumber
    });

I am not sure if I have explained it correctly. Basically I want to have the priceNumber uniquely match with the other data in the existing array, and not to be added as a separate index.

Advertisement

Answer

super_array[0].pret = priceNumber

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement