使用另一个对象数组过滤对象数组

时间:2018-04-23 12:19:56

标签: javascript arrays object filter lodash

我想用另一个对象数组过滤一个对象数组。逻辑将用于按类别和颜色等进行产品搜索。这是我的主要数组:

products: [{
                            id: 1,
                              name: 'Product 1',
                              category: 'Home',
                              skill: 'Easy',
                              color: 'blue',
                              price: 100.00
                            }, {
                                id: 2,
                              name: 'Product 2',
                              category: 'Home',
                              skill: 'Intermediate',
                              color: 'red',
                              price: 120.00
                            }, {
                                id: 3,
                              name: 'Product 3',
                              category: 'Office',
                              skill: 'Intermediate',
                              color: 'green',
                              price: 190.00
                            }, {
                                id: 4,
                              name: 'Product 4',
                              category: 'Office',
                              skill: 'Advanced',
                              color: 'blue',
                              price: 260.00
                            }, {
                                id: 5,
                              name: 'Product 5',
                              category: 'Warehouse',
                              skill: 'Advanced',
                              color: 'white',
                              price: 321.00
                            }, {
                                id: 6,
                              name: 'Product 6',
                              category: 'Farm',
                              skill: 'Intermediate',
                              color: 'red',
                              price: 120.00
                            }, {
                                id: 7,
                              name: 'Product 7',
                              category: 'Space',
                              skill: 'Advanced',
                              color: 'green',
                              price: 150.00
                            }, {
                                id: 8,
                              name: 'Product 8',
                              category: 'Bathroom',
                              skill: 'Easy',
                              color: 'black',
                              price: 9.00
                            }]

我正在创建的过滤器就像这个数组一样。

The filter I am creating on the fly like this array.

预期结果是按多个选定的类别和颜色过滤产品数据。

我尝试过以下代码:

var filtered = [];
                  for(var arr in self.products){
                     for(var filter in self.selectedFilters){
                         if(self.products[arr].category == self.selectedFilters[filter].category && self.products[arr].color == self.selectedFilters[filter].color){
                            filtered.push(self.products[arr]);
                           }
                     }
                  }
                  console.log(filtered);

2 个答案:

答案 0 :(得分:2)

      var filtered = [];

      for(var arr in myArray){
         for(var filter in myFilter){
             if(myArray[arr].userid == myFilter[filter].userid && myArray[arr].projectid == myFilter[filter].projectid){
                filtered.push(myArray[arr].userid);
               }
         }
      }
      console.log(filtered);

这可能对您有所帮助

先谢谢

答案 1 :(得分:2)

myArray = [
  {
    userid: "100",
    projectid: "10",
    rowid: "0"
  },
  {
    userid: "101",
    projectid: "11",
    rowid: "1"
  },
  {
    userid: "102",
    projectid: "11",
    rowid: "2"
  },
  {
    userid: "102",
    projectid: "13",
    rowid: "3"
  },
  {
    userid: "101",
    projectid: "10",
    rowid: "4"
  }
];

myFilter = [
  {
    userid: [
      {
        0: "101"
      },
      {
        1: "102"
      }
    ],
    projectid: [
      {
        0: "11"
      }
    ]
  }
];

const filterFn = (array, filter) => {
  let result = [];
  filter.forEach(element => {
    let keys = Object.keys(element);
    keys.forEach(key => {
      let values = Object.values(element[key]);
      values = values.map(x => Object.values(x)[0]);
      let ans = array.filter(e => {
        if (values.includes(e[key])) return e;
      });
      result = result.concat(ans);
    });
  });
  return result;
};
console.log(filterFn(myArray, myFilter));