我有一个帐户对象,例如:
{
"amount": "822370.71",
"state": "ME"
},
{
"amount": "968817.53",
"state": "FL"
},
{
"amount": "587603.26",
"state": "OH"
},
{
"amount": "657617.83",
"state": "OH"
},
{
"amount": "657617.83",
"state": "FL"
}
假设我想过滤掉状态为“FL”和“OH”的对象,我怎么能用filter()来做呢?感谢。
答案 0 :(得分:3)
试试这个
function filterArray (item) {
if (item.state === "OH" || item.state === "FL"){
return item;
}
}
var resultArray = yourArray.filter(filterArray);
console.log(resultArray);
答案 1 :(得分:1)
试试这个:
var array = [{
"amount": "822370.71",
"state": "ME"
},
{
"amount": "968817.53",
"state": "FL"
},
{
"amount": "587603.26",
"state": "OH"
},
{
"amount": "657617.83",
"state": "OH"
},
{
"amount": "657617.83",
"state": "FL"
}];
var filterArray = array.filter(function(item){
return item.state === 'FL' || item.state === 'OH';
});
console.log(filterArray);

答案 2 :(得分:0)
使用带有一个参数的函数的array.filter(callback)
并返回满足所需条件的值:
var testArray = [ {
"amount": "822370.71",
"state": "ME"
},
{
"amount": "968817.53",
"state": "FL"
},
{
"amount": "587603.26",
"state": "OH"
},
{
"amount": "657617.83",
"state": "OH"
},
{
"amount": "657617.83",
"state": "FL"
}];
var filtered = testArray.filter(isStateFLorOH);
和fucntion回调应该是这样的:
function isStateFLorOH(value){
if(value.state == "FL" || value.satate == "OH")
return value;
}