here is my data:
arrayA= [{"studentID":1,"Type":"A"},{"studentID":2,"Type":"B"},{"studentID":3,"Type":"C"},{"studentID":4,"Type":"A"}]
filteredArrayOrderlyOn = [{"studentID":1},{"Type":"A"}] (depending on the order the user selects the filters)
Output should be
arrayA = [{"studentID":1,"Type":"A"}]
or if the filteredArrayOrderlyOn array changes because user has control on this selection.
filteredArrayOrderlyOn = [{"Type":"B"},{"studentID":1}] then output should be nothing []
Or if
fillteredArrayOrderlyOn = [{"Type":"A"}]
then output should be
arrayA= [{"studentID":1,"Type":"A"},{"studentID":4,"Type":"A"}]
So i would like to filter ArrayA, in the correct order, meaning that in filteredArrayOrderly first the filter should be studentID=1 and then Type which is A.
i have been trying without any luck
newArray = arrayA.filter(function (item) {
return Object.keys(elem) === Object.keys(item);
});
})
or using lodash
newArray = _.filter(arrayA, function (elem) {
// return elem.Type=== filteredArrayOrderlyOn.Type || elem.studentID=== filteredArrayOrderlyOn.studentID
// });
but getting too many repetitions
thakns guys
Try this
let arrayA = [
{ "studentID": 1, "Type": "A" },
{ "studentID": 2, "Type": "B" },
{ "studentID": 3, "Type": "C" },
{ "studentID": 4, "Type": "A" }
];
let filteredArrayOrderlyOn = [
{ "studentID": 1 },
{ "Type": "A" }
];
let filteredArray = arrayA;
filteredArrayOrderlyOn.forEach(filter => {
const key = Object.keys(filter)[0];
const value = filter[key];
filteredArray = filteredArray.filter(item => item[key] === value);
});
console.log(filteredArray);