I am struggling with finding an efficient method to filter out items from an array of objects based on two specific attributes matching those of the last 6 items of another array. Currently, I have a manual process in place which results in cumbersome and lengthy code:
const filteredList = list.filter(x => {
return (
(x.someProperty === parseInt(anotherList[anotherList.length - 1][0].someProperty, 10) && x.anotherProperty === anotherList[anotherList.length - 1][0].anotherProperty) ||
(x.someProperty === parseInt(anotherList[anotherList.length - 2][0].someProperty, 10) && x.anotherProperty === anotherList[anotherList.length - 2][0].anotherProperty) ||
(x.someProperty === parseInt(anotherList[anotherList.length - 3][0].someProperty, 10) && x.anotherProperty === anotherList[anotherList.length - 3][0].anotherProperty) ||
//remaining comparisons for each of the last 6 items
)
}
Although this approach currently functions as intended, I am interested in exploring a more optimal solution that does not require explicitly listing and comparing each item of anotherList
. Is there a better way to achieve this filtering process?
Thank you.