【问题标题】:Rearrange array with new order using javascript [duplicate]使用javascript重新排列具有新顺序的数组[重复]
【发布时间】: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


【解决方案1】:

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"];


const updatedArray = data.map((item) => arr.find((t) => t.label === item));
console.log(updatedArray);

【讨论】:

    【解决方案2】:
    • 使用Array#reduce,遍历data,将元素索引保存在Map
    • 使用Array#sort,按照上面Map中每个元素的值对arr进行排序

    const sort = (arr = [], data = []) => {
      const indicesMap = data.reduce((map, e, i) => map.set(e, i), new Map);
      return [...arr].sort(({ label: a}, { label: b }) => {
        const indexA = indicesMap.get(a), indexB = indicesMap.get(b);
        return (indexA === undefined || indexB === undefined) 
          ? isNaN(indexA) - isNaN(indexB)
          : indexA - indexB;
      });
    }
    
    const arr = [ {label: 'a', width: 200}, {label: 'b', width: 200}, {label: 'c', width: 200}, {label: 'd', width: 200}, {label: 'e', width: 200} ];
    console.log( sort(arr, ['d', 'e']) );
    console.log( sort(arr, ['a', 'd']) );
    console.log( sort(arr, ['d', 'e', 'a', 'c', 'b']) );

    【讨论】:

      【解决方案3】:

      简单地说:

      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']
      
      arr.sort((a, b) => data.indexOf(a.label) - data.indexOf(b.label))
      
      console.log(arr)

      【讨论】:

        【解决方案4】:

        这是一个使用排序的简短解决方案

        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'];
        
        const sorted = arr.sort(function(a, b){  
          return data.indexOf(a.label) - data.indexOf(b.label);
        });
        
        console.log(sorted)

        【讨论】:

          【解决方案5】:

          要解决这个问题,请使用类似的 map() 和 find() 方法:

          const sorted = data.map(element =>  arr.find(obj => obj.label === element))
          

          【讨论】:

            【解决方案6】:

            可以将排序后的数组的顺序作为权重,对数组进行排序。

            const arr = [
               {label: 'a', width: 200},
               {label: 'b', width: 200},
               {label: 'c', width: 200},
               {label: 'd', width: 200},
               {label: 'e', width: 200}
            ];
            
            const sortOrder = ['d', 'e', 'a', 'c', 'b'];
            
            
            arr.sort((x, y) => sortOrder.indexOf(x.label) - sortOrder.indexOf(y.label))
            
            console.log(arr)

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-08-11
              • 2011-09-07
              • 2022-01-02
              • 2012-02-14
              • 1970-01-01
              相关资源
              最近更新 更多