我面临根据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)
建立的答案是 根据给定的索引重新排列数组
任何提示/解决方案都受到高度赞赏。
到底什么对您不起作用?
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)