'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



Solution 1:[1]

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.

Solution 2:[2]

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);

Solution 3:[3]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nizamuddin Shaikh
Solution 2 Abderrahmen Mhemed
Solution 3