Is there a one-line code to get an subarray from an array by index?
For example, suppose I want to get [\"a\",\"c\",\"e\"] from [\"a\",\"b\",\"c\"
[\"a\",\"c\",\"e\"]
[\"a\",\"b\",\"c\"
You can use Array.prototype.reduce()
const arr = ['a', 'b', 'c', 'd', 'e']; const indexes = [0, 2, 4]; const result = indexes.reduce((a, b)=> { a.push(arr[b]); return a; }, []); console.log(result);