[英]Sorting an array based on property value of another array of objects
Let's say I have an array of objects:假设我有一个对象数组:
var list = [
{ name: "A", distance: 1},
{ name: "B", distance: 2},
{ name: "C", distance: 3},
{ name: "D", distance: 4},
{ name: "E", distance: 5},
{ name: "F", distance: 6},
{ name: "G", distance: 7},
{ name: "H", distance: 8}
];
if I have another array like this one :如果我有另一个这样的数组:
var disturbed = ["G", "B", "C", "F"];
how can I sort disturbed array
based on distance property
from the list array
like this:如何根据
list array
distance property
对disturbed array
排序,如下所示:
["B", "C", "F", "G"];
Edit: I have tried this code with no success:编辑:我试过这个代码没有成功:
items = [
{ name: "A", distance: 1},
{ name: "B", distance: 2},
{ name: "C", distance: 3},
{ name: "D", distance: 4},
{ name: "E", distance: 5},
{ name: "F", distance: 6},
{ name: "G", distance: 7},
{ name: "H", distance: 8}
]
sorting = [ 1, 2,3,4,5,6,7,8,9,10 ];
result = []
for (let i = 0; i < items.length; i++){
sorting.forEach(function(key) {
var found = false;
items = items.filter(function(item) {
if(!found && items[i].distance == key) {
result.push(item);
found = true;
return false;
} else
return true;
})
})
result.forEach(function(item) {
document.writeln(item[i])
})
}
How can I Sort an array based on the property value of another array of objects如何根据另一个对象数组的属性值对数组进行排序
You can use .reduce() to change the list
array to object and then sort based on this object.您可以使用.reduce()将
list
数组更改为对象,然后根据此对象进行排序。
Demo:演示:
var list = [ { name: "A", distance: 1}, { name: "B", distance: 2}, { name: "C", distance: 3}, { name: "D", distance: 4}, { name: "E", distance: 5}, { name: "F", distance: 6}, { name: "G", distance: 7}, { name: "H", distance: 8} ]; var disturbed = ["G", "B", "C", "F"]; var sort = list.reduce((acc, cur) => { acc[cur.name] = cur.distance; return acc; }, {}); disturbed.sort((a, b) => sort[a] - sort[b]); console.log(disturbed)
You can use .find()
to find the object with the specified name
property that matches your elements in distributed
.您可以使用
.find()
来查找具有指定name
属性的对象,该属性与您在distributed
中的元素相匹配。 Once you have got this you can then get the distance property and calculate the difference to sort accordingly:一旦你得到了这个,你就可以得到距离属性并计算差异以进行相应的排序:
const list = [ { name: "A", distance: 1}, { name: "B", distance: 2}, { name: "C", distance: 3}, { name: "D", distance: 4}, { name: "E", distance: 5}, { name: "F", distance: 6}, { name: "G", distance: 7}, { name: "H", distance: 8} ]; const disturbed = ["G", "B", "C", "F"]; const res = disturbed.sort((a, b) => { const {distance: d_a} = list.find(({name}) => name === a); const {distance: d_b} = list.find(({name}) => name === b); return d_a - d_b; }); console.log(res);
A more efficient approach would be to create a new Map
using .map()
and then use .sort()
on the keys form the map:更有效的方法是使用
.map()
创建一个new Map
,然后在形成地图的键上使用.sort()
:
const list = [ { name: "A", distance: 1}, { name: "B", distance: 2}, { name: "C", distance: 3}, { name: "D", distance: 4}, { name: "E", distance: 5}, { name: "F", distance: 6}, { name: "G", distance: 7}, { name: "H", distance: 8} ]; const disturbed = ["G", "B", "C", "F"]; const lut = new Map(list.map(({name, distance}) => [name, distance])); const res = disturbed.sort((a, b) => lut.get(a) - lut.get(b)); console.log(res);
You can use the sort
method to do that您可以使用
sort
方法来做到这一点
var list = [
{ name: "A", distance: 1},
{ name: "B", distance: 2},
{ name: "C", distance: 3},
{ name: "D", distance: 4},
{ name: "E", distance: 5},
{ name: "F", distance: 6},
{ name: "G", distance: 7},
{ name: "H", distance: 8}
];
var disturbed = ["G", "B", "C", "F"];
disturbed.sort((a, b) => {
var itemA = list.find(item => item.name === a);
var itemB = list.find(item => item.name === b);
return itemA.distance - itemB.distance;
});
disturbed.forEach(function(item) {
document.writeln(item[i])
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.