我有以下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 );

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


当前回答

你可以很容易地做到这一点-可能有很多实现可供你选择,但这是我的基本想法(可能有一些格式,你可以用jQuery迭代一个对象,我只是现在想不起来):

function filter(collection, predicate)
{
    var result = new Array();
    var length = collection.length;

    for(var j = 0; j < length; j++)
    {
        if(predicate(collection[j]) == true)
        {
             result.push(collection[j]);
        }
    }

    return result;
}

然后你可以像这样调用这个函数:

filter(json, function(element)
{
    if(element.price <= 1000 && element.sqft >= 500 && element.num_of_beds > 2 && element.num_of_baths > 2.5)
        return true;

    return false;
});

这样,您可以根据定义的任何谓词调用筛选器,甚至可以使用更小的筛选器进行多次筛选。

其他回答

**

The simplest way to search a specific value in an Array of Object
--------------------------------------------------------------------

filter() -> uses a callback function the return value of which decides what will be returned in the filtered array. If the return value is true, the item is included in the resultant array.

includes() -> searches for something in an array of items using == equality
**

       const words = [{id:1,name:"tim"},{id:2,name:"morgon"}]
        
          function searchHandler(enteredString){
          const formatedString = enteredString.toLowerCase();
          const result = words.filter(data=> data?.name?.includes(formatedString));
          console.log(result);
    }

或者您可以简单地使用$。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);
    }
});

你可以使用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
});

用于搜索数组中对象的所有属性的高级代码

b=[]; 
yourArray.forEach(x => {
      Object.keys(x).forEach(i => {if (x[i].match('5') && !b.filter(y => y === x).length) { b.push(x) }})
    });
console.log(b)

你可以使用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);