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

178
Vistas
I have a problem entering data into an object using FOR

I have an object I want to go through using the FOR loop Because I want to change the object according to my needs

Here is my original object

I copied it from console.log()

(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]

[
 {Order: 14, Address: 100101, SKU: 'CJNSXZHL', CJ Quantity: '1', Store: 'go', …}
 {SKU: 'CJNSX', CJ Quantity: '1', CJ Product Name: 'bbb', __rowNum__: 2}
 {Order: 15, Address: 100102, SKU: 'CJNSXZN', CJ Quantity: '1', Store: 'go', …}
 {SKU: 'CJNS', CJ Quantity: '1', CJ Product Name: 'ddd', __rowNum__: 4}
 {Order: 16, Address: 100103, SKU: 'CJNSX', CJ Quantity: '1', Store: 'go', …}
 {SKU: 'CJNS', CJ Quantity: '1', CJ Product Name: 'fff', __rowNum__: 6}
{Order: 17, Address: 100104, SKU: 'CJNSTX', CJ Quantity: 2, Store: 'go', …}
]

Now what I'm trying to do is check if instead I in ORDER I have undefined You can see that in the first row it is set in the second no and if not then take the value found in SKU in the second row and insert it in the previous place in the array I will attach the code I wrote

const arr = [];
for ( let i=0 ; i <= items.length-1;i++) {
                
            try {
            if (items[i].Order !== undefined){
                console.log(" have ",i)
                arr.push({
                    "CJ Order Number":items[i].Order,
                    "Address":items[i].Address,
                    "SKU":[items[i].SKU],
                        });
            }
            else if (items[i].Order === undefined){ 
                
                console.log("dont have ",i)
                arr[i-1].SKU.push(items[i].SKU)
                
            }
            }catch (err){
                console.log("err",i)
            }
        }  
        console.log(arr)

What happens is only in the first line it succeeds But in the third iteration is supposed to enter the second row it does not succeed I get an error

"TypeError: Cannot read properties of undefined (reading 'SKU')"

And what's strange is that he succeeds in the first line here The code from console.log()

I opened the first line so you can see the SKU that he entered the value of the next line

(4) [{…}, {…}, {…}, {…}]
0:
Address: 100101
CJ Order Number: 14
SKU: (2) ['CJNSXZHL', 'CJNSX']
[[Prototype]]: Object
1: {CJ Order Number: 15, Address: 100102, SKU: Array(1)}
2: {CJ Order Number: 16, Address: 100103, SKU: Array(1)}
3: {CJ Order Number: 17, Address: 100104, SKU: Array(1)}
length: 4

I also followed step by step Of any integration


 have  0
 1 'dont have'
 have  2
 3 'dont have'
 err 3
 have  4
 5 'dont have'
 err 5
 have  6

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

you can do it with easily with reduce

const data = [{
    Order: 14,
    Address: 100101,
    SKU: 'CJNSXZHL',
    'CJ Quantity': '1',
    Store: 'go'
  },
  {
    SKU: 'CJNSX',
    'CJ Quantity': '1',
    'CJ Product Name': 'bbb',
    __rowNum__: 2
  },
  {
    Order: 15,
    Address: 100102,
    SKU: 'CJNSXZN',
    'CJ Quantity': '1',
    Store: 'go'
  },
  {
    SKU: 'CJNS',
    'CJ Quantity': '1',
    'CJ Product Name': 'ddd',
    __rowNum__: 4
  },
  {
    Order: 16,
    Address: 100103,
    SKU: 'CJNSX',
    'CJ Quantity': '1',
    Store: 'go'
  },
  {
    SKU: 'CJNS',
    'CJ Quantity': '1',
    'CJ Product Name': 'fff',
    __rowNum__: 6
  },
  {
    Order: 17,
    Address: 100104,
    SKU: 'CJNSTX',
    'CJ Quantity': 2,
    Store: 'go'
  }
]

const result = data.reduce((res, item) => {

  if (item.Order) {
    
    return [...res, {...item, SKU:[]}]
  }
  const lastOrder = res[res.length - 1]
  res[res.length - 1].SKU = [...(lastOrder.SKU || []), item]
  return res

}, [])

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Hopefully this is what you are trying to do, you could try doing it this way, there might be better solutions tho.

  const arr = [];
  const items = [
    {
      Order: 14,
      Address: 100101,
      SKU: "CJNSXZHL",
      CJ_Quantity: "1",
      Store: "go",
    },
    { SKU: "CJNSX", CJ_Quantity: "1", CJ_Product_Name: "bbb", __rowNum__: 2 },
    {
      SKU: "CJNSXZN",
      Order: 15,
      Address: 100102,
      CJ_Quantity: "1",
      Store: "go",
    },
    { SKU: "CJNS", CJ_Quantity: "1", CJ_Product_Name: "ddd", __rowNum__: 4 },
    { Order: 16, Address: 100103, SKU: "CJNSX", CJ_Quantity: "1", Store: "go" },
    { SKU: "CJNS", CJ_Quantity: "1", CJ_Product_Name: "fff", __rowNum__: 6 },
    { Order: 17, Address: 100104, SKU: "CJNSTX", CJ_Quantity: 2, Store: "go" },
  ];
  for (let i = 0, j = 1; i <= items.length - 1; i++) {
    try {
      if (items[i].Order) {
        arr.push({
          "CJ Order Number": items[i].Order,
          Address: items[i].Address,
          SKU: [items[i].SKU],
        });
      } else {
        arr[j-1].SKU.push(items[i].SKU);
        j++;
      }
    } catch (err) {
      console.log("err", err);
    }
  }
      console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

let items = [{
    Order: 14,
    Address: 100101,
    SKU: 'CJNSXZHL',
    'CJ Quantity': '1',
    Store: 'go',
  },
  {
    SKU: 'CJNSX',
    'CJ Quantity': '1',
    'CJ Product Name': 'bbb',
    __rowNum__: 2
  },
  {
    Order: 15,
    Address: 100102,
    SKU: 'CJNSXZN',
    'CJ Quantity': '1',
    Store: 'go',
  },
  {
    SKU: 'CJNS',
    'CJ Quantity': '1',
    'CJ Product Name': 'ddd',
    __rowNum__: 4
  },
  {
    Order: 16,
    Address: 100103,
    SKU: 'CJNSX',
    'CJ Quantity': '1',
    Store: 'go'
  },
  {
    SKU: 'CJNS',
    'CJ Quantity': '1',
    'CJ Product Name': 'fff',
    __rowNum__: 6
  },
  {
    Order: 17,
    Address: 100104,
    SKU: 'CJNSTX',
    'CJ Quantity': 2,
    Store: 'go'
  },
];

const arr = [];

for (let i = 0; i <= items.length - 1; i++) {

  if (items[i].Order) {
    arr.push({
      'CJ Order Number': items[i].Order,
      Address: items[i].Address,
      SKU: [items[i].SKU],
    });
  } else {
    arr[arr.length - 1].SKU.push(items[i].SKU); // the last element
  }
}

console.log(arr);
Try this.

about 4 years ago · Juan Pablo Isaza Denunciar
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