I need to filter array of object by another array of object without knowledge of exact properties in criterias array. Let's take a look to example for better understanding.
Here is an array that I need to filter
const dataset = [
{
id: "4",
someval1: "10",
someval2: "20",
someval3: "30",
someval4: "40"
},
{
id: "10",
someval1: "10",
someval2: "20",
someval3: "30",
someval4: "40"
},
{
id: "22",
someval1: "102",
someval2: "202",
someval3: "302",
someval4: "40"
}];
Here is an array which has values that are supposed to be filter condition for first array
const criterias = [{ someval1: "10" }, { someval3: "30" }, { someval4: "40" }];
So whenever object in dataset contains all values from criterias I want to keep him. Problem is that I want objects in dataset to be filtered by all criterias that are equal.
So far I was able to get this script which filters dataset correctly but only by one criteria. So from given arrays after filtering I should get only first two objects from dataset, third one does not meat all criterias.
Here is my current script
const dataset = [
{
id: "4",
someval1: "10",
someval2: "20",
someval3: "30",
someval4: "40"
},
{
id: "10",
someval1: "10",
someval2: "20",
someval3: "30",
someval4: "40"
},
{
id: "22",
someval1: "102",
someval2: "202",
someval3: "302",
someval4: "40"
}];
const criterias = [{ someval1: "10" }, { someval3: "30" }, { someval4: "40" }];
const filter = dataset.filter(item => criterias.some(criteria => Object.keys(criteria).some(prop => item[prop] != criteria[prop])));
console.log(filter)