Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: How to get values of an array at certain index positions

I have an array of values

arr = ["a","b","c","d"]

and I have another array of indexes

indexes = [0,2]

What is the best way to get the values of the array at these indexes ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If I apply the method to the values above

it should return

["a","c"]

>Solution :

Use Array.map:

arr = ["a","b","c","d"]

indexes = [0,2]

const res = indexes.map(e => arr[e])

console.log(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading