我试图避免与过滤器和地图链接,但是为什么我的reduce返回未定义?
const data = [{
value: "moto",
passed: true
},{
value: "boat",
passed: false
}, {
value: "car",
passed: true
}]
// expected ['moto', 'car']
// using filter and map
data.filter(obj => obj.passed).map(obj => obj.value)
怎么了?
data.reduce((accum, obj) => obj.checked && [...accum, obj.value], [])
答案 0 :(得分:2)
您可以这样做:
const data = [{value: "moto",passed: true}, {value: "boat",passed: false}, {value: "car",passed: true}];
const result = data.reduce((a, c) => c.passed ? (a.push(c.value), a) : a, []);
console.log(result);
答案 1 :(得分:1)
(accum, obj) => obj.checked && [...accum, obj.value]
如果未选中对象,则不返回过滤列表。
(accum, obj) => {
if (obj.checked) accum.push(obj.value)
return accum;
}
因为减速器功能会做到这一点。
或将其保留为一体,以保持可读性:
(accum, obj) => obj.checked ? [...accum, obj.value] : accum;
答案 2 :(得分:0)
这是因为如果obj.passed = yaxis: {
autotick: false,
ticks: 'outside',
color: 'white',
title: '****',
titlefont: 14,
tick0: 0,
dtick: 50,
},
,则不返回任何内容。更新如下。请检查
false