Home > OS >  Sort by size in an array of arrays with objects
Sort by size in an array of arrays with objects

Time:08-10

I'm trying to order by size, however, I don't understand why it doesn't work. Apparently the error must come from the "for in" since when testing it I can't iterate "products" completely.

let products = [
  [
    { product: "t1", price: 123, size: "S" },
    { product: "f1", price: 45, size: "XL" },
    { product: "t5", price: 910, size: "M" }
  ],
  [
    { product: "f2", price: 6.78, size: "L" },
    { product: "t2", price: 910, size: "XL" },
    { product: "t3", price: 910, size: "S" }
  ]
];

let obj = {
  sizesSM: [],
  sizesLXL: []
};

for (let i in products)
  for (let j in products) {
    if (products[i][j].size === "S" || products[i][j].size === "M")
      obj.sizesSM.push(products[i][j]);
    else 
      obj.sizesLXL.push(products[i][j]);
  }

console.log(obj);

The expected output is to save all sizes S and M on one side, and L and XL on the other in the object.

Thanks you!

CodePudding user response:

You can fill your sizesSM like this :

obj.sizesSM = products.flat().filter(product => product.size === "S" || product.size === "M")

You can adapt for the other array

Ps: By the way your inner loop is looping on the wrong array, it should loop on an inner array also

CodePudding user response:

It is not recommended to use the for in statement with an array.

Basically for in is used to iterate over an object.

You can use the for of statement or forEach method on arrays...

let products = [
  [
    { product: "t1", price: 123, size: "S" },
    { product: "f1", price: 45, size: "XL" },
    { product: "t5", price: 910, size: "M" }
  ],
  [
    { product: "f2", price: 6.78, size: "L" },
    { product: "t2", price: 910, size: "XL" },
    { product: "t3", price: 910, size: "S" }
  ]
];

let obj = {
  sizesSM: [],
  sizesLXL: []
};

products.flat().forEach(product => {
    if (product.size === 'S' || product.size === 'M') {
      obj.sizesSM.push(product)
    } else {
      obj.sizesLXL.push(product)
    }
})

console.log(obj);

CodePudding user response:

You need to replace

for (let j in products) {

with

for (let j in products[i]) {

and it will work.

let products = [
  [
    { product: "t1", price: 123, size: "S" },
    { product: "f1", price: 45, size: "XL" },
    { product: "t5", price: 910, size: "M" }
  ],
  [
    { product: "f2", price: 6.78, size: "L" },
    { product: "t2", price: 910, size: "XL" },
    { product: "t3", price: 910, size: "S" }
  ]
];

let obj = {
  sizesSM: [],
  sizesLXL: []
};

for (let i in products)
  for (let j in products[i]) {
    if (products[i][j].size === "S" || products[i][j].size === "M")
      obj.sizesSM.push(products[i][j]);
    else 
      obj.sizesLXL.push(products[i][j]);
  }

console.log(obj);


In addition to forloop, you can also use flat, filter and reduce to achieve.

let products = [
  [
    { product: "t1", price: 123, size: "S" },
    { product: "f1", price: 45, size: "XL" },
    { product: "t5", price: 910, size: "M" }
  ],
  [
    { product: "f2", price: 6.78, size: "L" },
    { product: "t2", price: 910, size: "XL" },
    { product: "t3", price: 910, size: "S" }
  ]
];

const sizeGroupBy = (...size) => 
  products.flatMap(a => a).filter(o => size.includes(o.size));

console.log('sizesSM', JSON.stringify(sizeGroupBy('S', 'M')));
console.log('sizesLXL', JSON.stringify(sizeGroupBy('L', 'XL')));

const restructure = arr => 
  arr.flat().reduce((accumu, current) => {
    if(['S', 'M'].includes(current.size)) {
       accumu['sizesSM'] = 
        [...accumu['sizesSM'] ?? '', current];
    }
    if(['L', 'XL'].includes(current.size)) {
       accumu['sizesLXL'] = 
        [...accumu['sizesLXL'] ?? '', current];
    }
    return accumu;
  }, {});
  
const obj = restructure(products);

for (const [key, value] of Object.entries(obj)) {
  console.log(key, JSON.stringify(value));
}

  • Related