我想使用其他数组的Javascript对JSON项进行排序,并按字母顺序对其余项进行排序。
我有一个我想要获取JSON项目的数组:
var order = [3,9,50,7];
带有" ID&#34的JSON;我想使用订单数组排序的密钥,以及使用" Name"的其他非匹配项目。请关键。
这是原始的JSON:
var data = [
{
"id": "9",
"title": "B"
},
{
"id": "63",
"title": "Z"
},
{
"id": "433",
"title": "D"
},
{
"id": "50",
"title": "A"
},
{
"id": "2",
"title": "G"
}
]
这是我希望它的最终结果:
var data = [
{
"id": "9",
"title": "B"
},
{
"id": "50",
"title": "A"
},
{
"id": "433",
"title": "D"
},
{
"id": "2",
"title": "G"
},
{
"id": "63",
"title": "Z"
}
]
答案 0 :(得分:2)
您可以使用对象作为排序顺序,使用默认值将其他ID移动到结尾。
var order = [3, 9, 50, 7],
data = [{ id: "9", title: "B" }, { id: "63", title: "Z" }, { id: "433", title: "D" }, { id: "50", title: "A" }, { id: "2", title: "G" }],
orderO = {};
order.forEach(function (a, i, aa) {
orderO[a] = i - aa.length; // use negative values and zero as default
});
data.sort(function (a, b) {
return (orderO[a.id] || 0) - (orderO[b.id] || 0) || a.title.localeCompare(b.title);
});
console.log(data);
console.log(orderO);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
首先找到data
数组中与order
数组匹配的项目。然后使用自定义排序算法对剩余项目进行排序,连接结果。
var order = [3, 9, 50, 7];
var data = [{ "id": "9", "title": "B" }, { "id": "63", "title": "Z" }, { "id": "433", "title": "D" }, { "id": "50", "title": "A" }, { "id": "2", "title": "G" }];
function sortArr(arr, order) {
var newArr = [];
for (var i = 0; i < order.length; i++) {
var index = arr.findIndex(x => x.id === order[i].toString());
if (index >= 0) {
newArr.push(arr.splice(index, 1));
}
}
return newArr.concat(arr.sort((a, b) => a.title > b.title ? 1 : -1));
}
console.log(sortArr(data, order));
&#13;
答案 2 :(得分:1)
您可以先将订单数组减少到一个对象,其中键是元素,值是数组中的索引。然后使用sort来按数组中的元素排序,然后按照它们在数组中的位置对它们进行排序,最后按标题对其他元素进行排序。
var data = [{"id":"9","title":"B"},{"id":"63","title":"Z"},{"id":"433","title":"D"},{"id":"50","title":"A"},{"id":"2","title":"G"}]
var order = [3,9,50,7].reduce(function(r, e, i) {
return r[e] = i, r
}, {})
var result = data.sort(function(a, b) {
return ((order[b.id] != undefined) - (order[a.id] != undefined) || (order[a.id] - order[b.id])) ||
a.title.localeCompare(b.title)
})
console.log(result)
&#13;
答案 3 :(得分:0)
试试这个:
var order = [3,9,50,7];
var data = [
{
"id": "9",
"title": "B"
},
{
"id": "63",
"title": "Z"
},
{
"id": "433",
"title": "D"
},
{
"id": "50",
"title": "A"
},
{
"id": "2",
"title": "G"
}
]
let sortedArray = data.sort((a, b) => {
return ((order.indexOf(a.id) || Number.Max_VALUE) - (order.indexOf(b.id) || Number.MAX_VALUE) || a.title.localeCompare(b.title) )
})
console.log(sortedArray)
答案 4 :(得分:0)
可以在一个排序功能中完成。使用Array.indexOf获取值以在order数组中对id进行排序。如果订单数组中只有一个项目,则总是向上移动。所有其他案例按字母顺序排序。
var order = [3,9,50,7];
var data = [
{id: "9", title: "B"},
{id: "63", title: "Z"},
{id: "433", title: "D"},
{id: "50", title: "A"},
{id: "2",title: "G"},
{id: "9",title: "F"},
{id: "7",title: "D"},
{id: "3",title: "E"},
{id: "2",title: "F"},
]
// the sort function
function sortFunc(a,b){
var iA = order.indexOf(Number(a.id))+1; // get index of A
var iB = order.indexOf(Number(b.id))+1; // get index of B
if(iA && iB){ // if both in the order array
return iA === iB ? // if the same in order array then sort alphabet
a.title.localeCompare(b.title) :
iA - iB;
}
// if A or B in order array move up else sort alphabet
return iA ? -1 : iB ? 1 : a.title.localeCompare(b.title);
}
// sort data
data.sort(sortFunc);
// dispay result
data.forEach(obj=>console.log(obj.id + ":" + obj.title))