只是想知道我应该坚持使用reduce进行映射吗?
代码很长。有没有一种方法可以使它看起来更整洁?
const items = [
{ Name: "one" },{ error: true,code: "INVALID 1" },{ Name: "two" },{ Name: "three" },code: "INVALID OTHER" },]
const filtered = items.reduce((acc,item) => {
if (item.error === true) {
if (!acc.errors) {
acc.errors = [];
}
acc.errors.push(item);
return acc;
}
if (!acc.items) {
acc.items = [];
}
acc.items.push(item);
return acc;
},{});
console.log(filtered)