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

126
Views
use array of indexes to filter another array javascript

I have 1 array and 1 set,set Indexes and array is Names.

Indexes contains various numbers which are basically indexes of random elements of Names array.I want to filter Names array based on indexes in Indexes array i.e push specific elements of Names array to another array whose indexes are present in the Indexes array.

What I am doing is:

const Indexes =[0,1]
const Names=["Test1","Test2","Test3","Test4"]

const filteredNames = Indexes.map(item => Names[item]);

Output should be:

filteredNames=["Test1","Test2"]

but its not working.Any leads on this?

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

0

If Indexes is a Set, then you just need Array.from function:

const Indexes =new Set([0,1])
const Names=["Test1","Test2","Test3","Test4"]

const filteredNames = Array.from(Indexes)
    .map(item => Names[item]);
about 4 years ago · Juan Pablo Isaza Report

0

You almost had it right

const Indexes =[0,1]
const Names=["Test1","Test2","Test3","Test4"]

const filteredNames = Indexes.map(item => Names[item]);
console.log(filteredNames)

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!