1
votes

I'm a newbie, struggling in this new world.

I have this array of objects:

[
  {
    "orderid": "12696",
    "id": 5180,
    "name": "Crema Chantilly 250 Ml",
    "price": 377.2,
    **"quantity": 1,**
    "order": 0,
    "category": 167
  },
  {
    "orderid": "12696",
    "id": 3929,
    "name": "Close Up",
    "price": 111.15,
    **"quantity": 2,**
    "order": 1,
    "category": 156
  },
  {
    "orderid": "12696",
    "id": 2739,
    "name": "Hellmanns 232 Ml",
    "price": 92.68,
    **"quantity": 2,**
    "order": 31,
    "category": 131
  }
]

These are product cards which will get scanned by a barcode scanner, for a warehouse picking app. I'm doing it as a sort of multi-step form for this. I need the objects to be multiplied by its quantity so, I can get to pick each item. I'm using sequelize to get everything from the database, and then a .map() to make a new array of objects of what i need from the product card.

This is an example of what i'm seeking:

[
  {
    "orderid": "12696",
    "id": 5180,
    "name": "Crema Chantilly 250 Ml",
    "price": 377.2,
    **"quantity": 1,**
    "order": 0,
    "category": 167
  },
  {
    "orderid": "12696",
    "id": 3929,
    "name": "Close Up",
    "price": 111.15,
    **"quantity": 2,**
    "order": 1,
    "category": 156
  },
  {
    "orderid": "12696",
    "id": 3929,
    "name": "Close Up",
    "price": 111.15,
    **"quantity": 2,**
    "order": 1,
    "category": 156
  },
  {
    "orderid": "12696",
    "id": 2739,
    "name": "Hellmanns 232 Ml",
    "price": 92.68,
    **"quantity": 2,**
    "order": 31,
    "category": 131
  },
  {
    "orderid": "12696",
    "id": 2739,
    "name": "Hellmanns 232 Ml",
    "price": 92.68,
    **"quantity": 2,**
    "order": 31,
    "category": 131
  }
]

I also thought of doing it all on the DOM, using a number input which will decrement as i scan the bar-code of the product, but I can't figure it out how to do it.

Thanks in advance, i would really appreciate your help !

3
@JaromandaX Someone ordered 12 items of a product, but, i wasnt going to copy and paste 12 times, i was trying to simulate that. The id is a fake barcode that i added to simulate a product code, which serve me as a validator to compare it to the code that the user inputs.Facu Martin
Seems like all you really need is another property for orderQty. Repeating items in an array is an unusual approach for something like this IMO. Then to fill that item in the order you have everything in one objectcharlietfl
@FacuMartin I see you have commented on all of the answers to say they were what you were looking for, If they helped, you could consider upvoting them as helpful and accepting one so the question is marked as resolved on the site. See What should I do when someone answers my question?FluffyKitten
@FluffyKitten oh sorry about that! I'll do that, thanks for the tip, i'm new here!Facu Martin
No problem. It is entirely up to you whether to upvote or accept any answers you get, but it's always nice to show appreciation for good answers that are helpful :)FluffyKitten

3 Answers

2
votes

What do you mean by "multiply an object by its quantity"? You want the object to appear that many times in the array? If so, you can use reduce:

const items = [
  {
    "orderid": "12696",
    "id": 5180,
    "name": "Crema Chantilly 250 Ml",
    "price": 377.2,
    "quantity": 1,
    "order": 0,
    "category": 167
  },
  {
    "orderid": "12696",
    "id": 3929,
    "name": "Close Up",
    "price": 111.15,
    "quantity": 5,
    "order": 1,
    "category": 156
  },
]

const flattened = items.reduce((acc, item) => {
  return [
    ...acc, // previous items
    ...Array.from({ length: item.quantity }, () => ({...item})) // current item repeated 'quantity' times
  ]
}, []);

console.log(flattened);
1
votes

Use Array#flatMap and Array.from

const input = [
  {
    "orderid": "12696",
    "id": 5180,
    "name": "Crema Chantilly 250 Ml",
    "price": 377.2,
    "quantity": 1,
    "order": 0,
    "category": 167
  },
  {
    "orderid": "12696",
    "id": 3929,
    "name": "Close Up",
    "price": 111.15,
    "quantity": 2,
    "order": 1,
    "category": 156
  },
  {
    "orderid": "12696",
    "id": 2739,
    "name": "Hellmanns 232 Ml",
    "price": 92.68,
    "quantity": 2,
    "order": 31,
    "category": 131
  }
];
const result = input.flatMap(i=>Array.from({length:i.quantity},()=>({...i})));
console.log(result);
1
votes

Oh I like ray hatfield's answer.

Mine is the same thing, but more verbose and less elegant!

// Prepare somewhere to store the results
const results = [];

// "mini-items" for brevity:
const items = [{id:1, q:1}, {id:2, q:3}, {id:3, q:2}]; 

for (let item of items){
    
    // repeat "quantity" times, and add to results
    for(i=0;i<item.q;i++) {
      // Make a copy of the item
      let copy = { ...item }; 

      // do something with the copy
      copy.someOtherId = Math.random();

      // Set the copy quantity
      copy.q = 1;

      results.push( copy );
    }
}
// all done!
console.log(results);

So basically: Loop through all the items, and once more for quantity in each. Make a copy and push it to results.