There You go. Just in toCompare* = list*.map(i => i.XXX) for XXX set what You are going to compare. For now it is set i.id witch compares id of each item in the array listA of objects with id in the array listB of objects
const compareArrayObjects = (listA, listB) => {
const toCompareA = listA.map(i => i.id)
const toCompareB = listB.map(i => i.id)
let list = []
toCompareB.some(i => {
const index = toCompareA.indexOf(i)
if(index >= 0) {
const item = listA[index]
item.id = 'matching id ' + item.id
// if you want to have list A with indicated what is matching
// list = [
// ...listA.slice(0, index),
// item,
// ...listA.slice(index + 1)
// ]
// or you want to have a new array with matching items
list.push(item.id)
}
})
return list
}
console.log(compareArrayObjects(listAofObjects, listBofObjects))
// to have returned list with no repetitions:
const delRepSort = (listA, listB) => {
const toCompareA = listA.map(i => i.id)
const toCompareB = listB.map(i => i.id)
let list = []
toCompareA.some(i => {
const index = toCompareB.indexOf(i)
item = listA[toCompareA.indexOf(i)]
if(index < 0) {
list.push(item)
}
})
return list
}