在特定的顺序对象的数组排序 [英] Sort array of objects in specific order
问题描述
我有对象,从API调用返回,我需要整理到一个特定格式的数组。
我试图组织 destination_country_id
按字母顺序排列的除了的前三个和最后一个项目。例如,像这样:
- 爱尔兰
- 英国
- 美的
- 的 ...等国家,按字母顺序... 的
- 其他地方
我已经考虑使用中的Array.sort()
,我明白,我可以轻松地使用它们按字母顺序排序,但我至今,一直不成功搞清楚如何我能达到所需的输出。
API响应
[
{
destination_country_id:空,
primary_cost:9.50,
REGION_ID:空,
destination_country_name:其他地方
},
{
destination_country_id:105,
primary_cost:8.00,
REGION_ID:空,
destination_country_name:联合王国,
},
{
destination_country_id:209,
primary_cost:9.50,
REGION_ID:空,
destination_country_name:美的,
},
{
destination_country_id:123,
primary_cost:5.00,
REGION_ID:空,
destination_country_name:爱尔兰,
},
{
destination_country_id:185,
primary_cost:5.00,
REGION_ID:空,
destination_country_name:法国,
},
{
destination_country_id:145,
primary_cost:5.00,
REGION_ID:空,
destination_country_name:西班牙,
}
]
也许并不是最有效的方法,但它是ES3,不需要任何库,并且是很容易理解的。同时假设你想在 destination_country_name
的JavaScript
//其中x是你的对象数组
x.sort(功能(A,B){
//字母顺序排列的一切
返回a.destination_country_name.localeCompare(b.destination_country_name);
})排序(功能(A,B){
//移动仅此于国家顶部
返回+(!b.destination_country_name.localeCompare('美国'));
})排序(功能(A,B){
//移动仅此于国家顶部
返回+(!b.destination_country_name.localeCompare('英国'));
})排序(功能(A,B){
//移动仅此于国家顶部
返回+(!b.destination_country_name.localeCompare('爱尔兰'));
})排序(功能(A,B){
//只能移动到这个国家到底
返回+(a.destination_country_name.localeCompare('其他地方')!);
});的console.log(JSON.stringify(X,['destination_country_name']));
输出
[{destination_country_name:爱尔兰},
{destination_country_name:英国},
{destination_country_name:美国},
{destination_country_name:法国},
{destination_country_name:西班牙},
{destination_country_name:其他地方}]
我们甚至可以更进一步,用上面的例子做一个可重复使用的功能,等等。
的JavaScript
函数分类器(数组,funcs中,订单){
funcs中= || funcs中{};
订单= ||订单{};
中的Array.sort(funcs.general);
如果(Array.isArray(orders.top)){
orders.top.slice()。反向()的forEach(函数(值){
中的Array.sort(funcs.top.bind(值));
});
} 如果(Array.isArray(orders.bottom)){
orders.bottom.forEach(函数(值){
中的Array.sort(funcs.bottom.bind(值));
});
} 返回数组;
}分拣机(X,{
一般:功能(A,B){
返回a.destination_country_name.localeCompare(b.destination_country_name);
},
顶部:功能(A,B){
返回+(b.destination_country_name.localeCompare(这一点)!);
},
底部:功能(A,B){
返回+(a.destination_country_name.localeCompare(这一点)!);
}
},{
顶:['爱尔兰','英国','美国'],
底部:其他地方]
});
现在你可以很容易地在排序不同属性在不同的比较功能,并定义值分析,应该是在顶部或底部。
我用ECMA5方法,但你可以很容易地与ECMA3做到这一点。
I have an array of objects returning from an API call which I need to sort into a specific format.
I'm trying to organise the destination_country_id
alphabetically except for the first three and last items. For example, like so:
- "Ireland"
- "United Kingdom"
- "United States"
- ...other countries, alphabetically...
- "Everywhere Else"
I have considered using array.sort()
, which I understand I can easily use to sort them alphabetically, but I've so far been unsuccessful in figuring out how I can achieve the desired output.
API Response
[
{
"destination_country_id":null,
"primary_cost":"9.50",
"region_id":null,
"destination_country_name":"Everywhere Else",
},
{
"destination_country_id":105,
"primary_cost":"8.00",
"region_id":null,
"destination_country_name":"United Kingdom",
},
{
"destination_country_id":209,
"primary_cost":"9.50",
"region_id":null,
"destination_country_name":"United States",
},
{
"destination_country_id":123,
"primary_cost":"5.00",
"region_id":null,
"destination_country_name":"Ireland",
},
{
"destination_country_id":185,
"primary_cost":"5.00",
"region_id":null,
"destination_country_name":"France",
},
{
"destination_country_id":145,
"primary_cost":"5.00",
"region_id":null,
"destination_country_name":"Spain",
}
]
Possibly not the most efficient method but it is ES3, doesn't require any libraries, and is fairly easy to understand. Also assuming you wanted to sort alphabetically on destination_country_name
Javascript
// where x is your array of objects
x.sort(function (a, b) {
// sorts everything alphabetically
return a.destination_country_name.localeCompare(b.destination_country_name);
}).sort(function (a, b) {
// moves only this to country to top
return +(!b.destination_country_name.localeCompare('United States'));
}).sort(function (a, b) {
// moves only this to country to top
return +(!b.destination_country_name.localeCompare('United Kingdom'));
}).sort(function (a, b) {
// moves only this to country to top
return +(!b.destination_country_name.localeCompare('Ireland'));
}).sort(function (a, b) {
// moves only this to country to bottom
return +(!a.destination_country_name.localeCompare('Everywhere Else'));
});
console.log(JSON.stringify(x, ['destination_country_name']));
Output
[{"destination_country_name":"Ireland"}, {"destination_country_name":"United Kingdom"}, {"destination_country_name":"United States"}, {"destination_country_name":"France"}, {"destination_country_name":"Spain"}, {"destination_country_name":"Everywhere Else"}]
On jsFiddle
We could even go a step further and use the above example to make a reusable function, like.
Javascript
function sorter(array, funcs, orders) {
funcs = funcs || {};
orders = orders || {};
array.sort(funcs.general);
if (Array.isArray(orders.top)) {
orders.top.slice().reverse().forEach(function(value) {
array.sort(funcs.top.bind(value));
});
}
if (Array.isArray(orders.bottom)) {
orders.bottom.forEach(function(value) {
array.sort(funcs.bottom.bind(value));
});
}
return array;
}
sorter(x, {
general: function (a, b) {
return a.destination_country_name.localeCompare(b.destination_country_name);
},
top: function (a, b) {
return +(!b.destination_country_name.localeCompare(this));
},
bottom: function (a, b) {
return +(!a.destination_country_name.localeCompare(this));
}
}, {
top: ['Ireland', 'United Kingdom', 'United States'],
bottom: ['Everywhere Else']
});
On jsFiddle
And now you can easily sort on different attributes by parsing in different compare functions, and define values that should be at the top or bottom.
I used ECMA5 methods but you could just as easily make it with ECMA3.
这篇关于在特定的顺序对象的数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!