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

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


当前回答

使用数组过滤数据

const pickupData = [
    {
      id: 2876635,
      pickup_location: "6311cdacf6b493647d86",
      address_type: null,
      address: "999, Jagarati",``
      address_2: "Vihar",
      updated_address: false,
      old_address: "",
      old_address2: "",
      city: "Meerut",
      state: "Uttar Pradesh",
      country: "India",
      pin_code: "250001",
      email: "938@gmail.com",
      is_first_mile_pickup: 0,
      phone: "76898",
      name: "Aa",
      company_id: 2889808,
      gstin: null,
      vendor_name: null,
      status: 2,
      phone_verified: 1,
      lat: null,
      long: null,
      warehouse_code: null,
      alternate_phone: null,
      rto_address_id: 2867270,
      lat_long_status: 0,
      new: 1,
      associated_rto_address: null
    },
    {
      id: 2872407,
      pickup_location: "6311cdad490cf6b493647d82",
      address_type: null,
      address: "Nagar",
      address_2: "Delhi",
      updated_address: false,
      old_address: "",
      old_address2: "",
      city: "Bijnor",
      state: "Uttar Pradesh",
      country: "India",
      pin_code: "246701",
      email: "ima@gmail.com",
      is_first_mile_pickup: 0,
      phone: "75398",
      name: "Amit Sharma",
      company_id: 2889808,
      gstin: null,
      vendor_name: null,
      status: 1,
      phone_verified: 1,
      lat: null,
      long: null,
      warehouse_code: null,
      alternate_phone: null,
      rto_address_id: 2867270,
      lat_long_status: 0,
      new: 1,
      associated_rto_address: null
    }
  ];

const shiprocketData = [ { line1: "999, Jagarati", line2: "Vihar", city: "Meerut", state: "Uttar Pradesh", pincode: 250001, country: "India", isCurrent: true, _id: "6311cdad490cf6b3647d86" }, { line1: "999, Jagarati", line2: "Vihar", city: "Meerut", state: "Uttar Pradesh", pincode: 250001, country: "India", isCurrent: true, _id: "6311cdad490cb493647d82" }, { line1: "999, Jagarati", line2: "Vihar", city: "Meerut", state: "Uttar Pradesh", pincode: 250001, country: "India", isCurrent: true, _id: "6311cdad490cf693647d89" } ];

const updatedData = () => {
    const data = pickupData.filter(
      (item, index) =>
        item.pickup_location === shiprocketData.map((item) => item._id)[index]
    );
    return console.log(data);
  };

其他回答

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

我更喜欢下划线框架。它提出了许多有用的对象操作。 你的任务:

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

可以像这样覆盖:

var newArray = _.filter (homes, function(home) {
    return home.price<=1000 && sqft>=500 && num_of_beds>=2 && num_of_baths>=2.5;
});

希望对大家有用!

var filterHome = homes.filter(home =>
  return (home.price <= 999 &&
         home.num_of_baths >= 2.5 &&
         home.num_of_beds >=2 &&
         home.sqft >= 998));
console.log(filterHome);

你可以用这个函数。更多的细节可以在这里找到,因为我们过滤的数据基于你有条件返回真或假,它将收集数据在不同的数组,所以你的实际数组将不会被修改。

@JGreig请调查一下。

**

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);
    }

你可以尝试使用像jLinq这样的框架——下面是一个使用jLinq的代码示例

var results = jLinq.from(data.users)
.startsWith("first", "a")
.orEndsWith("y")
.orderBy("admin", "age")
.select();

欲了解更多信息,请访问http://www.hugoware.net/projects/jlinq