javascript - How to display the quantity based on the size
问题描述
data=[{
color: 'red',
qty: '0',
_1x: '0',
_2x: '0',
_3x: '0',
_4x: '0',
xxs: '0',
xs: '0',
s: '14',
m: '0',
l: '0',
xl: '0'
},
{
color: 'pink',
qty: '13',
_1x: '0',
_2x: '0',
_3x: '0',
_4x: '0',
xxs: '0',
xs: '0',
s: '14',
m: '0',
l: '0',
xl: '0'
},
{
color: 'yellow',
qty: '16',
_1x: '0',
_2x: '0',
_3x: '0',
_4x: '0',
xxs: '0',
xs: '0',
s: '0',
m: '0',
l: '0',
xl: '12'
},
{
color: 'pink',
qty: '19',
_1x: '0',
_2x: '0',
_3x: '0',
_4x: '0',
xxs: '0',
xs: '0',
s: '0',
m: '0',
l: '0',
xl: '12'
}
];
const result1 = Object.values(
data.reduce((acc, { color, size, ...rest }) => {
if (!acc.hasOwnProperty(color)) {
acc[color] = {
color,
...rest
};
return acc;
}
Object.keys(rest).forEach(key => {
acc[color][key] = (+acc[color][key] || 0) + +rest[key];
});
return acc;
}, {})
);
for (var i = 0; i < result1.length; i++) {
var sum = 0;
for (var j = 0; j < SIZES.length; j++) {
sum += +result1[i][SIZES[j]];
}
result1[i]['Total'] = sum;
}
How to display the total quantity based on the size?
cause I want to do here is to sum the quantity based on same size and then display it based on the size.
for example color red then it size _2x
there's two data and it has a quantity 45
& 45
then it will sum on it 90
then it will display on the size _2x
with value 90
here's the code: https://stackblitz.com/edit/react-hook-bxfayy?file=index.js
expected
解决方案
首先过滤每种颜色items = data.filter(t => t.color == result1[i].color);
,然后您需要按每种尺寸过滤它(_2x ,..
)。之后,您必须选择qty
value bymap
并最终计算返回 value 的总和 by reduce
。
试试这个:
const SIZES=["_2x","_3x","_4x","l","m","s","xl","xs","xxs"],data=[{color:"red",qty:"45",_1x:"0",_2x:"12",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"0",m:"0",l:"0",xl:"0"},{color:"red",qty:"45",_1x:"0",_2x:"9",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"0",m:"0",l:"0",xl:"0"},{color:"red",qty:"45",_1x:"0",_2x:"0",_3x:"12",_4x:"0",xxs:"0",xs:"0",s:"0",m:"0",l:"0",xl:"0"},{color:"red",qty:"45",_1x:"0",_2x:"0",_3x:"12",_4x:"0",xxs:"0",xs:"0",s:"0",m:"0",l:"0",xl:"0"},{color:"red",qty:"65",_1x:"0",_2x:"0",_3x:"0",_4x:"12",xxs:"0",xs:"0",s:"0",m:"0",l:"0",xl:"0"},{color:"red",qty:"65",_1x:"0",_2x:"0",_3x:"0",_4x:"12",xxs:"0",xs:"0",s:"0",m:"0",l:"0",xl:"0"},{color:"pink",qty:"65",_1x:"0",_2x:"0",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"0",m:"0",l:"14",xl:"0"},{color:"red",qty:"68",qty:"0",_1x:"0",_2x:"0",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"0",m:"0",l:"14",xl:"0"},{color:"red",qty:"0",_1x:"0",_2x:"0",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"0",m:"14",l:"0",xl:"0"},{color:"red",qty:"0",_1x:"0",_2x:"0",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"0",m:"12",l:"0",xl:"0"},{color:"red",qty:"0",_1x:"0",_2x:"0",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"14",m:"0",l:"0",xl:"0"},{color:"pink",qty:"13",_1x:"0",_2x:"0",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"14",m:"0",l:"0",xl:"0"},{color:"yellow",qty:"16",_1x:"0",_2x:"0",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"0",m:"0",l:"0",xl:"12"},{color:"pink",qty:"19",_1x:"0",_2x:"0",_3x:"0",_4x:"0",xxs:"0",xs:"0",s:"0",m:"0",l:"0",xl:"12"}];
const result1 = Object.values(
data.reduce((acc, { color, size, ...rest }) => {
if (!acc.hasOwnProperty(color)) {
acc[color] = {
color,
...rest
};
return acc;
}
Object.keys(rest).forEach(key => {
acc[color][key] = (+acc[color][key] || 0) + +rest[key];
});
return acc;
}, {})
);
for (var i = 0; i < result1.length; i++) {
var items = data.filter(t => t.color == result1[i].color);
for (var j = 0; j < SIZES.length; j++) {
result1[i][SIZES[j]] = items.filter(t => t[SIZES[j]] > 0).map(t => t.qty).reduce((a, b) => +a + +b, 0);
}
}
console.log(result1)
推荐阅读
- javascript - Raspberry Node-js GPIO 控制
- javascript - Directus JS SDK 在 API 链接中添加了一个额外的斜线
- ios - 动态更改 CollectionView 的高度以适应内容大小时的错误结果
- c - 在二维矩阵中从递归函数转换为迭代函数
- rabbitmq - RabbitMQ Federation 链接数千个连接
- python - 根据另一个字段计算一个字段
- c# - Bind view-model property setter to control method
- c++ - 当您使用库中的抽象类时,如何解决“抽象类的深拷贝”问题?
- docker - 为什么 Flask-Cors 在生产中没有检测到我的跨域域?
- arrays - 更改 char[] 数组的元素