根据javaScript中给定的索引对数组重新排序

Zee*_*han 2 javascript arrays

我面临根据javaScript中给定索引更改数组的索引值的问题。我在StackOverflow上搜索并找到了一些解决方案,但那些不适用于我的解决方案不适用于javaScript。下面提到了输入数组和索引。

   Input:  arr[]   = [101, 102, 103];
   index[] = [1, 0, 2];
   Output: arr[]   = [102, 101, 103]
   index[] = [0,  1,  2] 
Run Code Online (Sandbox Code Playgroud)

建立的答案是 根据给定的索引重新排列数组

php-根据第二个给定数组对数组进行排序

根据键重新排列数组

根据另一个数组的顺序对一个数组进行排序

任何提示/解决方案都受到高度赞赏。

lib*_*bik 6

到底什么对您不起作用?

const arr = [101, 102, 103];
const index = [1, 0, 2];

const output = index.map(i => arr[i]);
console.log(output);
Run Code Online (Sandbox Code Playgroud)

  • 注意某些版本的浏览器可能存在的兼容性问题,主要是箭头功能https://caniuse.com/#feat=arrow-functions (2认同)