映射减少对象数组的计数和总和
map reduce count and sum of array of objects
我有一个对象数组,这些对象具有布尔值属性和一些具有整数值的属性。例如
const arr = [{
_id: "621bb15de2ecadf024da51d3",
draw: false,
pixel: false,
tooltip: 1,
points: 1,
},
{
_id: "621bb15de2ecadf024da51d8",
draw: false,
pixel: true,
tooltip: 0,
points: 1,
},
{
_id: "621bb15de2ecadf024da51da",
draw: false,
pixel: true,
tooltip: 1,
points: 1,
},
{
_id: "621bb15de2ecadf024da51e5",
draw: false,
pixel: true,
tooltip: 0,
points: 1,
},
{
_id: "621bb15de2ecadf024da51f8",
draw: false,
pixel: true,
tooltip: 1,
points: 1,
},
{
_id: "621bb15ee2ecadf024da5222",
draw: false,
pixel: true,
tooltip: 0,
points: 1,
},
{
_id: "621bb15ee2ecadf024da5230",
draw: false,
pixel: true,
tooltip: 1,
points: 1,
},
{
_id: "621bb15fe2ecadf024da52f3",
draw: false,
pixel: false,
tooltip: 1,
points: 1,
},
{
_id: "621bb160e2ecadf024da5375",
draw: false,
pixel: true,
tooltip: 0,
points: 1,
}
]
我正在努力实现 tooltip
和 points
的总和,以及 draw
和 pixel
的计数,它们是真实的。
const res = arr.reduce(function (
acc,
curr
) {
return {
tooltip: acc.tooltip + curr.tooltip,
points:
acc.points +
curr.points,
};
});
我得到的输出如下,总和是正确的,
{
points: 9,
tooltip: 5
}
但我还想要 draw
和 pixel
的计数,其中它的值为真。
Expected Result:
{
points: 9,
tooltip: 5,
pixel: 7,
draw: 0
}
只需使用与 tooltip
和 point
相同的方式,因为在 2 个布尔值之间使用 +
运算符,这些布尔值将被强制转换为整数
const arr = [ { _id: '621bb15de2ecadf024da51d3', draw: false, pixel: false, tooltip: 1, points: 1, }, { _id: '621bb15de2ecadf024da51d8', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15de2ecadf024da51da', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15de2ecadf024da51e5', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15de2ecadf024da51f8', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15ee2ecadf024da5222', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15ee2ecadf024da5230', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15fe2ecadf024da52f3', draw: false, pixel: false, tooltip: 1, points: 1, }, { _id: '621bb160e2ecadf024da5375', draw: false, pixel: true, tooltip: 0, points: 1, }, ]
const res = arr.reduce(function (acc, curr) {
return {
tooltip: acc.tooltip + curr.tooltip,
points: acc.points + curr.points,
draw: acc.draw + curr.draw,
pixel: acc.pixel + curr.pixel,
}
})
console.log(res)
参考资料
你可以试试这个:
let countPixel = 0;
let countDraw = 0;
const res = arr.reduce(function (
acc,
curr
) {
if (curr.pixel == true) {
countPixel++;
}
if (curr.draw == true) {
countDraw++;
}
return {
tooltip: acc.tooltip + curr.tooltip,
points: acc.points + curr.points,
pixel: countPixel,
draw: countDraw,
};
});
我有一个对象数组,这些对象具有布尔值属性和一些具有整数值的属性。例如
const arr = [{
_id: "621bb15de2ecadf024da51d3",
draw: false,
pixel: false,
tooltip: 1,
points: 1,
},
{
_id: "621bb15de2ecadf024da51d8",
draw: false,
pixel: true,
tooltip: 0,
points: 1,
},
{
_id: "621bb15de2ecadf024da51da",
draw: false,
pixel: true,
tooltip: 1,
points: 1,
},
{
_id: "621bb15de2ecadf024da51e5",
draw: false,
pixel: true,
tooltip: 0,
points: 1,
},
{
_id: "621bb15de2ecadf024da51f8",
draw: false,
pixel: true,
tooltip: 1,
points: 1,
},
{
_id: "621bb15ee2ecadf024da5222",
draw: false,
pixel: true,
tooltip: 0,
points: 1,
},
{
_id: "621bb15ee2ecadf024da5230",
draw: false,
pixel: true,
tooltip: 1,
points: 1,
},
{
_id: "621bb15fe2ecadf024da52f3",
draw: false,
pixel: false,
tooltip: 1,
points: 1,
},
{
_id: "621bb160e2ecadf024da5375",
draw: false,
pixel: true,
tooltip: 0,
points: 1,
}
]
我正在努力实现 tooltip
和 points
的总和,以及 draw
和 pixel
的计数,它们是真实的。
const res = arr.reduce(function (
acc,
curr
) {
return {
tooltip: acc.tooltip + curr.tooltip,
points:
acc.points +
curr.points,
};
});
我得到的输出如下,总和是正确的,
{
points: 9,
tooltip: 5
}
但我还想要 draw
和 pixel
的计数,其中它的值为真。
Expected Result:
{
points: 9,
tooltip: 5,
pixel: 7,
draw: 0
}
只需使用与 tooltip
和 point
相同的方式,因为在 2 个布尔值之间使用 +
运算符,这些布尔值将被强制转换为整数
const arr = [ { _id: '621bb15de2ecadf024da51d3', draw: false, pixel: false, tooltip: 1, points: 1, }, { _id: '621bb15de2ecadf024da51d8', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15de2ecadf024da51da', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15de2ecadf024da51e5', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15de2ecadf024da51f8', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15ee2ecadf024da5222', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15ee2ecadf024da5230', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15fe2ecadf024da52f3', draw: false, pixel: false, tooltip: 1, points: 1, }, { _id: '621bb160e2ecadf024da5375', draw: false, pixel: true, tooltip: 0, points: 1, }, ]
const res = arr.reduce(function (acc, curr) {
return {
tooltip: acc.tooltip + curr.tooltip,
points: acc.points + curr.points,
draw: acc.draw + curr.draw,
pixel: acc.pixel + curr.pixel,
}
})
console.log(res)
参考资料
你可以试试这个:
let countPixel = 0;
let countDraw = 0;
const res = arr.reduce(function (
acc,
curr
) {
if (curr.pixel == true) {
countPixel++;
}
if (curr.draw == true) {
countDraw++;
}
return {
tooltip: acc.tooltip + curr.tooltip,
points: acc.points + curr.points,
pixel: countPixel,
draw: countDraw,
};
});