我希望跳过 sparx,因为它的价格不可用。
这是打印 245NA9530 但我想要 370.
let shoes = [
{ id:205, name:'reebok', price:50 },
{ id:206, name:'adidas', price:45 },
{ id:207, name:'nike', price:150 },
{ id:207, name:'sparx', price:'NA' },
{ id:208, name:'clarks', price:95 },
{ id:209, name:'bata', price:30 },
];
console.log(shoes.reduce((n, {price}) => n + price, 0));
要跳过 NA,您可以添加一个条件:
console.log(shoes.reduce((n, {price}) => typeof price === 'number' ? n + price : n, 0));