如何从对象数组中过滤掉一个值?

时间:2017-09-11 13:06:00

标签: javascript

如何从对象数组中删除值" nissan"?

const cars = [
   {type: "audi", amount: 2},
   {type: "nissan", amount: 12},
   {type: "mazda", amount: 5},
   {type: "volvo", amount: 5}
]

这是我显示所有类型和金额的方式:

cars.map(car => {
   return (
     <div>{car.type}: {car.amount}</div>
   );
});

但我希望在映射汽车之前删除价值&#34; nissan&#34;,我怎么能用.filter做到这一点?

removeNissan() {
   // cars.type !== "nissan"
}

cars.filter(removeNissan).map(car => {
   return (
     <div>{car.type}: {car.amount}</div>
   );
});

1 个答案:

答案 0 :(得分:1)

var jsVar=0; function myFunction() { document.sampleForm.variable.value = jsVar; document.forms["sampleForm"].submit(); } 功能

之前使用Array#filter功能
map

或者如果您想使用命名函数,请声明您的函数

const cars = [
   {type: "audi", amount: 2},
   {type: "nissan", amount: 12},
   {type: "mazda", amount: 5},
   {type: "volvo", amount: 5}
]


cars.filter(item => item.type !== 'nissan').map(car => {
   return (
     <div>{car.type}: {car.amount}</div>
   );
});

filter会将每个项目作为参数传递给函数。