根据item'的属性将数组拆分为数组的数组
Break an array to array of arrays based on the some of item's property
我使用lodash,我想根据它的属性宽度的总和来分组对象数组。
我有这个数据集
[
{id: 0, width: 2},
{id: 1, width: 2},
{id: 2, width: 4},
{id: 3, width: 1},
{id: 4, width: 3}
]
实际上这是建立行和列,一行的最大宽度是4
预期输出为:
[
[{id: 0, width: 2}, {id: 1, width: 2}],
[{id: 2, width: 4}],
[{id: 3, width: 1}, {id: 4, width: 3}]
]
我该怎么做?
我的想法是:
const maxWidth = 4
let currentRowWidth = 0
let newData = _.map(source, data => {
let array = []
if (currentRowWidth === 0) {
currentRowWidth += data.width
...
...
}
})
边界情况:
- 例如当前行宽度为3,下一列宽度为2…然后它应该移动到下一行,因为如果我们加3和2…
。
[
[{id: 0, width: 1}, {id: 1, width: 2}],
[{id: 2, width: 2} ...],
]
function split(arr, max) {
max = max || 4;
var sum = 0;
return arr.reduce(function(rows, item) {
// If the next column can't fit in the current row, create a new row.
if (item.width + sum > max) {
sum = 0;
rows.push([]);
}
sum += item.width;
rows[rows.length - 1].push(item);
return rows;
}, [[]]);
}
看起来像一个CSS行/列系统。:)这是我想到的一个尝试。还没有经过测试,但应该非常接近。
function makeRows(flatArray) {
var output = [[]];
var currentRowWidth = 0;
for (var i = 0; i < flatArray.length; i++) {
if (currentRowWidth + flatArray[i].width < 4) {
// it fits.
currentRowWidth += flatArray[i].width;
output[output.length - 1].push(flatArray[i];
}
else if (flatArray[i].width <= 4) {
// needs a new row
output.push([]);
output[output.length-1].push(flatArray[i];
currentRowWidth = flatArray[i].width
}
else {
// handle your error for a row that's too wide to fit.
}
}
return output;
}
如果你想从功能上实现它,map()将无法真正工作,因为它一次只知道一个元素。你需要reduce,它会跟踪它的输出并逐块构建。
source.reduce((memo, item) => {
if (memo.currentRowWidth + item.width <= 4) {
memo.currentRowWidth += item.width;
memo.output[memo.output.length -1].push(item);
}
else if (item.width < 4) {
memo.push([]);
memo.output[memo.output.length -1].push(item);
memo.currentRowWidth += item.width;
}
else {
// item.width too big, do what you want
}
}, { currentRowWidth: 0, output: [[]] })
再一次,未经测试,我的头,但类似的
我想这可能是答案:
var data = [{ id: 0, width: 2 }, { id: 1, width: 2 }, { id: 1, width: 2 }, { id: 2, width: 4 }, { id: 3, width: 1 }, { id: 4, width: 3 }],
max = 4,
group = [],
grouped = [group,],
width = 0;
data.forEach(d => {
if (width + d.width > max) {
width = 0;
grouped.push(group = []);
}
width += d.width;
group.push(d);
});
console.log(grouped)
一个使用数组计数的提议,它使用所有空格,而不仅仅是最后一个槽。
var data = [{ id: 0, width: 2 }, { id: 1, width: 2 }, { id: 2, width: 4 }, { id: 3, width: 1 }, { id: 4, width: 3 }],
max = 4,
grouped = [];
data.forEach((count => a => {
var index = count.findIndex(b => b + a.width <= max);
if (index === -1) {
index = grouped.push([]) - 1;
count[index] = 0;
}
grouped[index].push(a);
count[index] += a.width;
})([]));
console.log(grouped);
相关文章:
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 需要帮助设置json数组
- 不能从angular2中的子组件指定父组件中的数组
- 使用JS将数组转换为json对象
- 数组在递归方法中设置为null
- knockoutjs可观察数组
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 将数组从PHP传递到Javascript
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- KnockoutJS从可观察数组中删除项目.Item 是 ul 中的列表项,由 foreach 生成
- 使用push-on-item值合并两个json数组
- 从数组中移除Item会在object的位置留下undefined
- JavaScript中根据item's的索引值将多个数组转换为不同的数组
- 如果value存在于数组2中,则从数组1中删除Item
- MobX计算在item实际插入数组之前运行
- 使用 Javascript 从数组中删除 Item
- 根据item'的属性将数组拆分为数组的数组
- 通过匹配ID检查json数组中是否已经存在item
- jQuery/JavaScript: Push item到最小满'row'在多维数组中