简体   繁体   English

如何根据另一个数组在Javascript中重新排序此数组?

[英]How do I reorder this array in Javascript based on another array?

real_order = [ '1', '2', '3', '4'];

friends = [ { name: 'jess', id: '4'},
            { name: 'alex', id: '1'},
            { name: 'kat', id: '3' },
            { name: 'bob', id: '2' }
          ] 

How do I make "friends" array "match" the elements in real_order ? 如何让“friends”数组“匹配” real_order的元素? The result should be: 结果应该是:

[ 
            { name: 'alex', id: '1'},
            { name: 'bob', id: '2' },
            { name: 'kat', id: '3' },
            { name: 'jess', id: '4'},
          ] 

What is the most efficient solution? 什么是最有效的解决方案?

Here is some code that would do it: 以下是一些可以执行此操作的代码:

var i, d = {}, result = [];
for(i=0; i<friends.length; ++i)
{
    d[friends[i].id] = friends[i];
}

for(i=0; i<real_order.length; ++i)
{
    result.push(d[real_order[i]]);
}

What this does is it creates a dictionary keyed on each of the friends' id, then uses the second array to do a look up and construct the new array. 这样做是因为它创建了一个键入每个朋友id的字典,然后使用第二个数组进行查找并构造新数组。 The resulting reordered array is stored in result. 生成的重新排序数组存储在result中。

Arrays can be sorted using your own custom sort algorithm, so you don't really need real_order . 可以使用您自己的自定义排序算法对数组进行排序,因此您不需要real_order This is the way I'd do it ( edit : added sort delegate for sorting descending): 这是我的方式( 编辑 :添加排序委托以排序降序):

var friends = [
           { id:4, name: 'jess'},
           { id:1, name: 'alex'},
           { id:3, name: 'kat' },
           { id:2, name: 'bob' }
];

var order = function(a,b,desc){
  return desc ? b.id - a.id : a.id - b.id;

},
orderDesc: function(a,b){return order(a,b,true);};

var friendsOrdered = friends.sort( order );
alert(friendsOrdered[0].name); //=> alex
alert(friendsOrdered[3].name); //=> jess

//or sort descending
var friendsOrdered = friends.sort( orderDesc );
alert(friendsOrdered[0].name); //=> jess
alert(friendsOrdered[3].name); //=> alex

make sure that real_order is in global scope and this should do it: 确保real_order在全局范围内,这应该这样做:

friends.sort(function(a, b) {
    if (real_order.indexOf(a.id) > real_order.indexOf(b.id)) {
        return 1;
    }else{
        return -1;
    }
});

One command solution. 一个命令解决方案 Ugly like jQuery, but people like John Resig love this style for some reason :) 像jQuery一样难看,但像John Resig这样的人出于某种原因喜欢这种风格:)

friends.sort(
  (function(order){
     return function(a, b) {
       return order.indexOf(a.id)-order.indexOf(b.id);
     }
  })(real_order)
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于JavaScript中的另一个数组比较和更新此数组? - How do I compare and update this array based on another array in JavaScript? 如何基于Angular JS中的另一个数组重新排序 - How to reorder based on another array in Angular JS Javascript:根据另一个数组对大型多维数组重新排序 - Javascript: reorder a large multidimensional array based on another array 如何根据另一个数组的索引在一个数组中找到一个数组? - How do I find an array in an array based on index of another array? 如何基于数组使用 HTML 和 JavaScript 重新排序图像和描述? - How to reorder images and descriptions using HTML and JavaScript based on an array? 如何在 JavaScript 中转换和重新排序数组? - How to transform and reorder array in JavaScript? 如何根据 JavaScript 中的另一个数组对 object 数据的数组进行分组、重组和计数 - How do I group, restructure and count array of object data based on another array in JavaScript 如何使用Javascript基于另一个数组的相应元素的属性对数组进行排序? - How do I sort an array based on the attribute of corresponding elements of another array using Javascript? 如何基于另一个对象数组对一个对象数组进行排序 - How do I sort an array of objects based on another array of objects 如何根据另一个数组的顺序对对象数组进行排序? - How do I sort an array of objects based on the ordering of another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM