简体   繁体   English

如何将数组拆分为给定大小的块? (JavaScript)

[英]How to split array into chunks of given size? (JavaScript)

I'm trying to work out how I can get such senario to work:我正在尝试弄清楚如何让这样的 senario 工作:

MEAL DEAL: $4.99 - Total 3 items
Must includ 1 of each "DRINK, SANCKS, SANDWICH"

I have this array:我有这个数组:

[
  { "CAT": "DRINKS", "QTY": 1, "PRICE": 1.2 },
  { "CAT": "DRINKS", "QTY": 1, "PRICE": 1.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 2.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 3.5 },
  { "CAT": "SANCKS", "QTY": 1, "PRICE": 1.5 },
  { "CAT": "SANDWICH", "QTY": 1, "PRICE": 5.3 },
  { "CAT": "SANDWICH", "QTY": 1, "PRICE": 3.0 }
]

Based on array above, I need to split it into chunks of 3 elements to create a meal deal.基于上面的数组,我需要将它分成3 个元素的块来创建餐点交易。

Looking at array above (7 elements) we can only have 2x Meal deals with 1 item "SNACK" that can't be part of the meal deal just yet because its missing other 2 necessary elements.查看上面的数组(7 个元素),我们只能有 2x 餐食交易,其中 1 件“零食”还不能成为餐食交易的一部分,因为它缺少其他 2 个必要元素。

I have tried the forEach loop but it only works for initial meal deal and than rest it dont work out.我已经尝试过 forEach 循环,但它只适用于初始膳食交易,而不是 rest 它不起作用。

var PER_CATEGORY_SET ={}
var ITM = "";
MY_ARRAY.forEach(function (my_item){
    var xx = MY_ARRAY.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});
           MY_ARRAY.splice(0, 1);
        }
        xx--;
    }
});
console.log(PER_CATEGORY_SET);

Any help would be very much appreciated, I have tried searching for examples to help me work out how I can do it myself (I'm not looking for a copy & paste code)任何帮助将不胜感激,我已经尝试搜索示例来帮助我自己解决问题(我不是在寻找复制和粘贴代码)

You could group the objects, based on cat and get a cartesian product of it.您可以根据cat对对象进行分组并获得它的笛卡尔积。

 var data = [{ CAT: "DRINKS", QTY: 1, PRICE: 1.2 }, { CAT: "DRINKS", QTY: 1, PRICE: 1.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 2.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 3.5 }, { CAT: "SANCKS", QTY: 1, PRICE: 1.5 }, { CAT: "SANDWICH", QTY: 1, PRICE: 5.3 }, { CAT: "SANDWICH", QTY: 1, PRICE: 3 }], result = Object.values(data.reduce((r, o) => ((r[o.CAT] = r[o.CAT] || []).push(o), r), {})).reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), [])); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

You could group each object based on the CAT value.您可以根据CAT值对每个 object 进行分组。 Get which of the groups have the smallest number of items.获取哪些组的项目数量最少。 Create a meals of that length and take items from each index of the each group创建该长度的餐点并从每个组的每个索引中获取项目

 const input=[{CAT:"DRINKS",QTY:1,PRICE:1.2},{CAT:"DRINKS",QTY:1,PRICE:1.5},{CAT:"SANCKS",QTY:1,PRICE:2.5},{CAT:"SANCKS",QTY:1,PRICE:3.5},{CAT:"SANCKS",QTY:1,PRICE:1.5},{CAT:"SANDWICH",QTY:1,PRICE:5.3},{CAT:"SANDWICH",QTY:1,PRICE:3}]; const group = input.reduce((acc, o) => { acc[o.CAT] = acc[o.CAT] || []; acc[o.CAT].push(o); return acc; }, {}) const groupedArrays = Object.values(group), possibleMeals = Math.min(...groupedArrays.map(a => a.length)), meals = Array.from({ length: possibleMeals }, (_, i) => groupedArrays.map(g => g[i])) console.log(meals)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM