1

I want to Filter an array of objects with another array of object.The logic will be used in product search by category and color etc. This is my main Array :

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.

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

The expected result is to filter product data by multiple selected categories and colors.

I have tried the following code :

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);
3
  • 2
    What have tried ?! , show us some code Commented Apr 23, 2018 at 12:20
  • Take a look here, might give you some ideas. Commented Apr 23, 2018 at 12:21
  • Well for every combination of user_id and project_id in my-filter, filter elements from my-array. You may push all filter results to another variable. Finally remove all duplicate elements. Commented Apr 23, 2018 at 12:26

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Naor, This works but I need intersected results only. In this case, if I pass this filter It should return [ { /**id:2**/ "userid": "101", "projectid": "11", "rowid": "1" }, { /**id:3**/ "userid": "102", "projectid": "11", "rowid": "2" }] I have updated my question also with exact array
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);

5 Comments

You can put a couple of array methods to use here - filter and some. They're available in all recent browsers, and there are polyfills available for the older browsers.
Here is the demo link with pure javascript. check this also jsfiddle.net/LznL6tr5/1
thanks man, But somehow I am getting myFilter[filter].userid as undefined
hai @NabaragPaul here you have to give a exact array. if this answer is usefull for you. please vote for this answer
Sorry for the uncleared question sir. I have edited the question with the exact Array and the code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.