我有以下JavaScript数组的房地产家对象:

var json = {
    'homes': [{
            "home_id": "1",
            "price": "925",
            "sqft": "1100",
            "num_of_beds": "2",
            "num_of_baths": "2.0",
        }, {
            "home_id": "2",
            "price": "1425",
            "sqft": "1900",
            "num_of_beds": "4",
            "num_of_baths": "2.5",
        },
        // ... (more homes) ...     
    ]
}

var xmlhttp = eval('(' + json + ')');
homes = xmlhttp.homes;

我想做的是能够对对象执行筛选,以返回“home”对象的子集。

例如,我希望能够基于:price、sqft、num_of_beds和num_of_baths进行过滤。

我如何在JavaScript中执行下面的伪代码:

var newArray = homes.filter(
    price <= 1000 & 
    sqft >= 500 & 
    num_of_beds >=2 & 
    num_of_baths >= 2.5 );

注意,语法不必完全像上面那样。这只是一个例子。


当前回答

这个问题是在考虑多种结果的情况下提出的,在这种情况下,过滤器是可行的方法,正如这里的其他回答者已经指出的那样。

然而,由于这个问题已经成为一个流行的重复目标,我应该提到,如果您正在寻找满足条件的单个元素,您不需要过滤器,而是可以使用find。它以同样的方式工作,但它只是返回第一个匹配的元素,如果没有元素匹配则返回undefined,而不是返回一个匹配数组:

const data = [
  { id: 1, value: 10 },
  { id: 2, value: 20 },
  { id: 3, value: 30 }
]

console.log(data.filter(o => o.value > 15))
// Output: [{ id: 2, value: 20 }, { id: 3, value: 30 }]

console.log(data.find(o => o.value > 15))
// Output: { id: 2, value: 20 }

console.log(data.filter(o => o.value > 100))
// Output: []

console.log(data.find(o => o.value > 100))
// Output: undefined

// `find` is often useful to find an element by some kind of ID:
console.log(data.find(o => o.id === 3))
// Output: { id: 3, value: 30 }

其他回答

你可以使用jQuery.grep()自jQuery 1.0:

$.grep(homes, function (h) {
  return h.price <= 1000
    && h.sqft >= 500
    && h.num_of_beds >= 2
    && h.num_of_baths >= 2.5
});

或者您可以简单地使用$。Each(它也适用于对象,而不仅仅是数组),并像这样构建一个新数组:

var json = {
    'homes': [{
            "home_id": "1",
            "price": "925",
            "sqft": "1100",
            "num_of_beds": "2",
            "num_of_baths": "2.0",
        }, {
            "home_id": "2",
            "price": "1425",
            "sqft": "1900",
            "num_of_beds": "4",
            "num_of_baths": "2.5",
        },
        // ... (more homes) ...     
        {
            "home_id": "3-will-be-matched",
            "price": "925",
            "sqft": "1000",
            "num_of_beds": "2",
            "num_of_baths": "2.5",
        },
    ]
}

var homes = [];
$.each(json.homes, function(){
    if (this.price <= 1000
        && this.sqft >= 500
        && this.num_of_beds >= 2
        && this.num_of_baths >= 2.5
    ) {
        homes.push(this);
    }
});

你可以使用forEach

const filterOutputs = [];
json.homes.forEach((home) => {
if (
parseInt(home.price) <= 1000 &&
parseInt(home.sqft) >= 500 &&
parseInt(home.num_of_beds) >= 2 &&
parseInt(home.num_of_baths) >= 2
) {
filterOutputs.push(home);
}
});

console.log(filterOutputs);

使用过滤器

var json = { homes: [{ "home_id": "1", "price": "925", "sqft": "1100", "num_of_beds": "2", "num_of_baths": "2.0", }, { "home_id": "2", "price": "1425", "sqft": "1900", "num_of_beds": "4", "num_of_baths": "2.5", }, ] } let filter = json.homes.filter(d => d.price >= 1000 & d.sqft >= 500 & d.num_of_beds >=2 & d.num_of_baths >= 2.5 ) console.log(filter)

你可以自己实现一个过滤方法来满足你的需求,下面是如何实现的:

function myfilter(array, test){
    var passedTest =[];
    for (var i = 0; i < array.length; i++) {
       if(test( array[i]))
          passedTest.push(array[i]);
    }

    return passedTest;
}

var passedHomes = myfilter(homes,function(currentHome){
     return ((currentHome.price <= 1000 )&& (currentHome.sqft >= 500 )&&(currentHome.num_of_beds >=2 )&&(currentHome.num_of_baths >= 2.5));
});

希望有帮助!