-1

I've been trying to get my head around this but I've had no luck at all. I've tried various ways, one of it being the forEach array method but I'm not able to get the output I need.

Idea is, MEAL DEAL: $2.99 - must be a combination of 1x Sandwich, 1x drink and 1x quick bite.

If it's just 1x each of the items in array, it works but when there are more than 1 of each item in array, I cant get it to work

I have the following array but it may vary:

[
  { "CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.2 },
  { "CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.5 },
  { "CAT": "QUICK BITES", "QTY": 1, "PRICE": 2.5 },
  { "CAT": "QUICK BITES", "QTY": 1, "PRICE": 3.5 },
  { "CAT": "SANDWICHES", "QTY": 1, "PRICE": 5.3 }
]

I need to split that into parts like below:

IN MEAL DEAL
[
  { "CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.2 },
  { "CAT": "QUICK BITES", "QTY": 1, "PRICE": 2.5 },
  { "CAT": "SANDWICHES", "QTY": 1, "PRICE": 5.3 }
]

NOT IN MEAL DEAL (IF SANDWICH IS ADDED, IT BECOMES MEAL DEAL)
[
  { "CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.5 },
  { "CAT": "QUICK BITES", "QTY": 1, "PRICE": 3.5 }
]

Sorry, if the explanation is not too good.

Idea is, each chunk must have all 3 element with following values. 1x COLD DRINK 1x SANDWICH 1x QUICK BITES

What I've tried:

var ITM ="";
var PER_CATEGORY_SET = [];
var value = [
      { "CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.2 },
      { "CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.5 },
      { "CAT": "QUICK BITES", "QTY": 1, "PRICE": 2.5 },
      { "CAT": "QUICK BITES", "QTY": 1, "PRICE": 3.5 },
      { "CAT": "SANDWICHES", "QTY": 1, "PRICE": 5.3 },
      { "CAT": "SANDWICHES", "QTY": 1, "PRICE": 7.5 }
    ]

value.forEach(function (my_item){
var xx = value.length;

while (xx > 0) {
   if(ITM !=  my_item.CAT ){
   ITM = my_item.CAT;PER_CATEGORY_SET.push({'CAT':my_item.CAT,'QTY':my_item.QTY,'PRICE':my_item.PRICE});
   value.splice(0, 1);
 }
   xx--;
  }
});

Output I get is below, it work to create only initial array than it don't do anything:

 [
      { "CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.2 },
      { "CAT": "QUICK BITES", "QTY": 1, "PRICE": 2.5 },
      { "CAT": "SANDWICHES", "QTY": 1, "PRICE": 5.3 }
 ]
Mr V
  • 9
  • 4

3 Answers3

0

I hope it helps you

let arr = [
 {"CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.2}, 
 {"CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.5},
 {"CAT": "QUICK BITES", "QTY": 1, "PRICE": 2.5},
 {"CAT": "QUICK BITES", "QTY": 1, "PRICE": 3.5},
 {"CAT": "SANDWICHES", "QTY": 1, "PRICE": 5.3},
 {"CAT": "SANDWICHES", "QTY": 1, "PRICE": 7.5}
]

let arr1 = [], arr2 = []

for (let i = 0; i < arr.length; i++ ){
  if(i%2==0) {
  arr1.push(arr[i])
  } else {
  arr2.push(arr[i])
  }
}

console.log(arr1,arr2)
Dima Vak
  • 599
  • 5
  • 20
  • thank you, would this method work regardless of initial array size? – Mr V May 31 '20 at 21:44
  • @MrV yes. Mark this as answer if it helps you. – Dima Vak May 31 '20 at 21:46
  • I just tried it, for the example it works great with defined array but when you have mor eelements it fails – Mr V May 31 '20 at 21:52
  • @MrV Are you sure about it? Can you say what a problem? – Dima Vak May 31 '20 at 21:54
  • Example I have array with 4x cold drinks 4x sandwich and 4x quick bit, I need 4 chunks dynamically created. Each chunk should contain 1x Cold Drink, 1x Sandwich, 1x quick bit. I'm not sure if im able to explain it properly. – Mr V May 31 '20 at 21:55
  • Where in question do you say about 4 chunks? All case have the same count oh drink, sandwich, and bit? 1x1x1, 3x3x3, 7x7x7? Or 3drink, 5 sandwiches, 4 bit too real? – Dima Vak May 31 '20 at 21:58
  • I'm just editing the question, give me few mins, sorry for this – Mr V May 31 '20 at 22:00
  • If i have 4x drink, 3x sandwich and 2x bite, than 2 array should be created as it would have all 3 emements. Meal deal $2.99 - buy 1x drink, 1x sandwich 1x bite So, when someone buys more than 1 meal deal items, the calculation works it out – Mr V May 31 '20 at 22:16
0

Create 2 new arrays and loop through the original array once. If i is even, then push in first array, else push in second array.

const originalArray =  [
    {"CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.2}, 
    {"CAT": "COLD DRINKS", "QTY": 1, "PRICE": 1.5},
    {"CAT": "QUICK BITES", "QTY": 1, "PRICE": 2.5},
    {"CAT": "QUICK BITES", "QTY": 1, "PRICE": 3.5},
    {"CAT": "SANDWICHES", "QTY": 1, "PRICE": 5.3},
    {"CAT": "SANDWICHES", "QTY": 1, "PRICE": 7.5}
    ]

 var firstChunk = [];
 var secondChunk = [];

 for(var i = 0; i<originalArray.length; i++){
     if(i%2 === 0){
         firstChunk.push(originalArray[i]);
     }
     else{
         secondChunk.push(originalArray[i]);
     }
 }

You can access both chucks through firstChunk and secondChunk respectively

  • It works when its a simple array but when its misture of items, it dont work. I'm trying to achieve how to create meal deal array chunks - ive re-worded the question. – Mr V May 31 '20 at 22:31
0

Lodash already has a chunk function written for exactly this: Lodash Chunk Docs

Jacob
  • 1,697
  • 8
  • 17