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?

2
  • Could the larger array be turned into a map (Object Literal)? Commented Oct 10, 2019 at 10:56
  • @elad.chen yes it could Commented Oct 10, 2019 at 10:58

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.