I have two arrays that need to be compared for matching elements.
let firstArray = [1, 2, 3]
let secondArray = [{id:1}, {id:1}, {id:3}]
I am trying to create a new array containing objects with the same id.
Despite trying different approaches, I am unable to achieve the desired outcome.
Update:
After receiving helpful input from flyingfox, I realized that my current solution does not account for duplicate values in the arrays. For example:
let firstArray = [1, 3, 3, 3]
let secondArray = [{id:1}, {id:2},{id:3}]
firstArray = firstArray.filter(item1 => secondArray.some(item2 => item2.id === item1))
secondArray = secondArray.filter(item1 => firstArray.some(item2 => item2 === item1.id))
Currently, the secondArray only contains [{id:1}, {id:3}], but I need to include duplicates as well.
The desired output for secondArray should be [{id:1}, {id:3}, {id:3}, {id:3}] since 3 appears multiple times in the firstArray.