Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
objects not properly pushed in array

The code below is supposed to decompose a json object sent from Postman into smaller objects that will be stored in a array. The problem is that if I console.log the result, I do see each and every objects decomposed as expected but the array.push method only push the last element of the inner array :

app.post('/commande',async(req,res)=>{

if( await authentification(req.query.login, req.query.pass)){

  var data = req.body.data; // array containing json object to be posted
  var dataLength = data.length;
  var bigObj= []; //array to store objects
  for (var i = 0; i<dataLength; i++) {

    var orderLines = data[i].orderLines;//array of orders
    var info ={};// object unit
    info.CptClient = data[i].clientAccount;
    info.customerOrderNumber= data[i].customerOrderNumber;
    info.orderLabel = data[i].orderLabel;
    var shipTo = data[i].shipTo;
    info.recepientName = shipTo.recepientName;
    info.contactName = shipTo.contactName;
    info.ad1 = shipTo.ad1;
    info.ad2 = shipTo.ad2;
    info.ad3 = shipTo.ad3;
    info.postalCode = shipTo.postalCode;
//"etc..."

  //
  for (var j = 0; j<orderLines.length;j++) {
    
    info.itemRef = orderLines[j].itemRef;
    info.itemQty = orderLines[j].itemQty;
    info.unitPrice = orderLines[j].unitPrice;
    
       
    
    console.log(info);//displays unique orderLabel :      ABC01 ABC02 ABC03 XYZ01 XYZ02 XYZ03
    bigObj.push(info); // stores only last of each type : ABC03 ABC03 ABC03 XYZ03 XYZ03 XYZ03
 
}
  }
res.json(bigObj)
}else {
  res.send("not authorized");
}

})

As explained in the comments, the console.log displays the right information as the objects are being created but the push method somehow would only push the last element of the orderLines array. Is there somebody to explain this phenomenon? Any idea? Thank you.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda