0

I've two arrays and one of them is too large. And the other one's max length is 9. What I tried to achieve is, I want to find items in the larger array by giving small array's items.

I did something like that;

const largeArray = [
  { id: 23 },
  { id: 12 },
  { id: 43 },
  { id: 54 },
  { id: 15 },
  //and so on and all ids are unique
]

const smallArray = [23, 12, 43]


smallArray.map(smallArrayItem => {
  largeArray.map(largeArrayItem => {
    if (smallArrayItem === largeArrayItem.id) {
      console.log(largeArrayItem)
    }
  })
})

But IMO that is not an efficient way. It's very slow. It takes almost 2 seconds to find the items. How do I make faster this search in a proper way?

Kevin S
  • 37
  • 1
  • 5

1 Answers1

0

Please use filter and includes

const largeArray = [
  { id: 23 },
  { id: 12 },
  { id: 43 },
  { id: 54 },
  { id: 15 },
]

const smallArray = [23, 12, 43]

const diff = largeArray.filter(item => !smallArray.includes(item.id));

console.log(diff);
Jay
  • 2,826
  • 2
  • 13
  • 28