代码之家  ›  专栏  ›  技术社区  ›  xoomer

使用javascript以新顺序重新排列数组

  •  0
  • xoomer  · 技术社区  · 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'];
    

    它需要根据新数据重新排列第一个数组。

    我应该在javascript中使用什么函数?

    编辑:谢谢你的评论。但更复杂的是,我们假设数据也可能不是第一个数组的完整列表。

    const data = ['a', 'c'];
    

    它仍然应该产生第一个数组,其中前两个元素是a&c、 剩下的是b,d,e。 完成的数组应该在a、c、b、d、e的列表中。

    4 回复  |  直到 4 年前
        1
  •  1
  •   Majed Badawi    4 年前

    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']) );
        2
  •  1
  •   Muhammad Atif Akram    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 updatedArray = data.map((item) => arr.find((t) => t.label === item));
    console.log(updatedArray);
        3
  •  1
  •   Mister Jojo Łukasz Daniel Mastalerz    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']
    
    arr.sort((a, b) => data.indexOf(a.label) - data.indexOf(b.label))
    
    console.log(arr)
        4
  •  1
  •   Elis Joaquim    4 年前

    要解决这个问题,请使用map()和find()方法,如下所示:

    const sorted = data.map(element =>  arr.find(obj => obj.label === element))
    
        5
  •  0
  •   J_K    4 年前

    这里有一个使用sort的简短解决方案

    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)
        6
  •  0
  •   xdeepakv    4 年前

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

    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)
    推荐文章