[removed] get subarray from an array by indexes

前端 未结 4 1335
执笔经年
执笔经年 2021-01-03 05:51

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\"

4条回答
  •  没有蜡笔的小新
    2021-01-03 06:31

    You can use filter

    const arr = ['a', 'b', 'c'];
    const indexes = [0, 2];
    
    const result = arr.filter((elt, i) => indexes.indexOf(i) > -1);
    
    document.body.innerHTML = result;

提交回复
热议问题