这个问题在这里已经有了答案:
javascript: get subarray from an array by indexes
(4 个回答)
3年前关闭。
我面临根据javascript中给定的索引更改数组索引值的问题。我在 StackOverflow 上搜索并找到了一些解决方案,但那些不适合我的解决方案不适用于 javaScript。提到了以下输入数组和索引。
Input: arr[] = [101, 102, 103];
index[] = [1, 0, 2];
Output: arr[] = [102, 101, 103]
index[] = [0, 1, 2]
有根据的答案是
reorder array according to given index
php - sort an array according to second given array
Rearrange an array according to key
Sorting an Array according to the order of another Array
任何类型的提示/解决方案都受到高度赞赏。
最佳答案
什么对你不起作用?
const arr = [101, 102, 103];
const index = [1, 0, 2];
const output = index.map(i => arr[i]);
console.log(output);
关于javascript - 根据javascript中的给定索引对数组重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52077428/