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

标签 javascript arrays

这个问题在这里已经有了答案:





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/

相关文章:

javascript - 如何在不重新加载 HTML 页面的情况下重定向 anchor 标记链接?

ios - 如何访问以字符串为键、任意数组为值的字典内容?

c++ - 编写一个函数的原型(prototype),该函数需要一个正好是 16 个整数的数组

javascript - 去抖动功能单元测试

Javascript - 动态从数组中获取元素

javascript - 推送到 GH 页面时 IndexRoute 不显示

javascript - 根据字符串路径生成div元素

arrays - Ruby:连接数组中的字符串对

arrays - 填充 UITableView 节标题标题,因为节 == 2?

javascript - javascript中的函数调用总是一个方法吗