Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda