Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

68
Views
how to filter an array by index which is given as an array

I want to filter this array by index?

arr = [1,2,3,4,5,6];

I am trying to use

arr.filter(x => x === [2,3])

but it does not seems to work?

I want only [2, 3]rd index?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need map here rather than filter:

arr = [11,22,33,44,55,66];
indexes = [2,3]
result = indexes.map(i => arr[i])
console.log(result)

Note that this returns values in the indexes order, but if indexes are unsorted and you need values in the source order, then you indeed need filter. Consider:

arr = [11,22,33,44,55,66];
indexes = [4,2]

result1 = indexes.map(i => arr[i])
result2 = arr.filter((_,i) => indexes.includes(i))

console.log(result1)
console.log(result2)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!