[英]How to add numbers when they have same category?
到目前为止我正在尝试的代码
let data = [
{size: 1180160, category: "Keswick", index: 1},
{size: 1059328, category: "HCOPA", index: 2},
{size: 30720, category: "HCOPA", index: 3},
{size: 493056, category: "Darhan Saluja", index: 4},
{size: 267776, category: "CRSA", index: 5},:
{size: 328704, category: "Arundel", index: 6}
{size: 73216, category: "Arundel", index: 7}
data.forEach((product, index) => {
let size = 0;
if (product.category !== lastCategory) {
size = product.size + size;
rows.push(
<ProductCategoryRow size={size}
category={product.category}
key={product.category}/>
);
}
rows.push(
<ProductRow
product={product}
key={product.name} index={index}/>
);
lastCategory = product.category;
});
我要计算具有相同类别的尺寸。 这样就可以作为道具的传递对象
我建议您首先创建一个新对象,其中包含所需的所有数据以及所需的属性的总和,然后使用该对象执行所需的逻辑。
请查看下面的代码段,并根据需要进行调整。
let data = [ {size: 1180160, category: "Keswick", index: 1}, {size: 1059328, category: "HCOPA", index: 2}, {size: 30720, category: "HCOPA", index: 3}, {size: 493056, category: "Darhan Saluja", index: 4}, {size: 267776, category: "CRSA", index: 5}, {size: 328704, category: "Arundel", index: 6}, {size: 73216, category: "Arundel", index: 7}] const dataWithSum = data.reduce((acc, current) => { acc[current.category] = acc[current.category] ? acc[current.category] + current.size : current.size; return acc }, {}) console.log(dataWithSum)
您可以创建数据块,然后将其呈现为
let data = [
{size: 1180160, category: "Keswick", index: 1},
{size: 1059328, category: "HCOPA", index: 2},
{size: 30720, category: "HCOPA", index: 3},
{size: 493056, category: "Darhan Saluja", index: 4},
{size: 267776, category: "CRSA", index: 5},
{size: 328704, category: "Arundel", index: 6},
{size: 73216, category: "Arundel", index: 7}]
const chunk = data.reduce((acc, item) => {
if(acc[item.category]) {
acc[item.category] = acc[item.category].concat(item)
} else {
acc[item.category] = [item];
}
return acc
}, {})
Object.keys(chunk).forEach((category, index) => {
rows.push(
<ProductCategoryRow
size={size}
category={category}
key={category}/>
);
chunk[category].forEach((product, index)) => {
rows.push(
<ProductRow
product={product}
key={product.name}/>
);
}
});
使用reduce
和findIndex
。 findIndex
将用于查找新数组是否具有与类别匹配的任何对象,如果匹配则更新大小,否则添加类别名称及其大小
let data = [{ size: 1180160, category: "Keswick", index: 1 }, { size: 1059328, category: "HCOPA", index: 2 }, { size: 30720, category: "HCOPA", index: 3 }, { size: 493056, category: "Darhan Saluja", index: 4 }, { size: 267776, category: "CRSA", index: 5 }, { size: 328704, category: "Arundel", index: 6 }, { size: 73216, category: "Arundel", index: 7 } ] var reduceCat = data.reduce(function(acc, curr) { // find the index of the category // return -1 if the category is not present var ifCatAvailable = acc.findIndex(function(item) { return curr.category === item.category; }) // if category is not present then // add the category and it's size if (ifCatAvailable === -1) { let locObj = { category: curr.category, size: curr.size } acc.push(locObj) // if catgory is present then update it's size } else { acc[ifCatAvailable].size += curr.size } return acc; }, []); console.log(reduceCat)
您还可以创建类别数组并将结果放入数组结果= [类别,大小]
let data = [
{size: 1180160, category: "Keswick", index: 1},
{size: 1059328, category: "HCOPA", index: 2},
{size: 30720, category: "HCOPA", index: 3},
{size: 493056, category: "Darhan Saluja", index: 4},
{size: 267776, category: "CRSA", index: 5},
{size: 328704, category: "Arundel", index: 6},
{size: 73216, category: "Arundel", index: 7}];
let categories = [];
let result = [];
data.forEach((product, index) => {
if( !categories.includes(product.category) ) {
categories.push(product.category);
result.push({
category : product.category,
size: product.size
});
}
else{
result.find(x => x.category === product.category).size += product.size;
}
});
console.log(data, categories, result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.