【发布时间】:2023-04-02 11:39:01
【问题描述】:
我有一个数组
const arr = [
{label: 'a', width: 200},
{label: 'b', width: 200},
{label: 'c', width: 200},
{label: 'd', width: 200},
{label: 'e', width: 200}
];
给定另一个数组
const data = ['d', 'e', 'a', 'c', 'b'];
需要根据新数据重新排列第一个数组。
我应该在javascript中使用什么函数?
编辑:感谢您提供非常有趣的 cmets。但为了更复杂,我们假设 data 也可能不是第一个数组的完整列表。
const data = ['a', 'c'];
它仍然应该输出第一个数组,其中前两个元素是 a 和 c,其余元素是 b、d、e。 完成的数组应该在 a、c、b、d、e 的列表中。
【问题讨论】:
-
有几种可能性。您可以使用
data.map将每个字母映射到arr的相应对象。您可以为arr中的对象分配索引,然后使用arr.sort按该索引排序。 ...
标签: javascript arrays reorderlist