我有一个json数组和其他数组用于排序顺序。现在我想根据排序数组项对json数组进行排序。
var checkedIds =["2","1","4"];//sort order array
//json array
var jsonData1= {
"ProductImages": [
{ "checkBoxField": false, "itemId": 1, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 2, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 3, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 4, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 5, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 6, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 7, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 8,"rateValue":"$34" }
]
};
输出应该是
var jsonData1= {
"ProductImages": [
{ "checkBoxField": true, "itemId": 2, "rateValue":"$34" },
{ "checkBoxField": true, "itemId": 1, "rateValue":"$34" },
{ "checkBoxField": true, "itemId": 4, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 3, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 5, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 6, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 7, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 8,"rateValue":"$34" }
]
};
答案 0 :(得分:1)
您可以使用哈希表作为订单和默认值,大小足以将优先级值移动到最后。
var checkedIds = ["2", "1", "4"],
jsonData1 = { "ProductImages": [{ "checkBoxField": false, "itemId": 1, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 2, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 3, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 4, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 5, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 6, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 7, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 8, "rateValue": "$34" }] };
jsonData1.ProductImages.sort(function (hash) {
checkedIds.forEach(function (a, i) {
hash[a] = i + 1;
});
return function (a, b) {
return (hash[a.itemId] || Infinity) - (hash[b.itemId] || Infinity);
};
}(Object.create(null)));
console.log(jsonData1);

版本更新checkBoxField
并短路。
var checkedIds = ["2", "1", "4"],
jsonData1 = { "ProductImages": [{ "checkBoxField": false, "itemId": 1, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 2, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 3, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 4, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 5, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 6, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 7, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 8, "rateValue": "$34" }] },
hash = Object.create(null);
checkedIds.forEach(function (a, i) {
hash[a] = i + 1;
});
jsonData1.ProductImages.some(function (a, i) {
if (a.itemId in hash) {
a.checkBoxField = true;
return !--this.count;
}
}, { count: checkedIds.length });
jsonData1.ProductImages.sort(function (a, b) {
return (hash[a.itemId] || Infinity) - (hash[b.itemId] || Infinity);
});
console.log(jsonData1);

答案 1 :(得分:1)
您可以根据计算的排名进行自定义排序:
var checkedIds = ["2", "1", "4"]; //sort order array
//json array
var jsonData1={ProductImages:[{checkBoxField:!1,itemId:1,rateValue:"$34"},{checkBoxField:!1,itemId:2,rateValue:"$34"},{checkBoxField:!1,itemId:3,rateValue:"$34"},{checkBoxField:!1,itemId:4,rateValue:"$34"},{checkBoxField:!1,itemId:5,rateValue:"$34"},{checkBoxField:!1,itemId:6,rateValue:"$34"},{checkBoxField:!1,itemId:7,rateValue:"$34"},{checkBoxField:!1,itemId:8,rateValue:"$34"}]};
function getRank(item){
var index = checkedIds.indexOf(item.itemId.toString());
var rank = 1;
if (index>-1){
rank *= (checkedIds.length - index) * -1;
}
return rank;
}
jsonData1.ProductImages.sort(function(a,b){
var r1 = getRank(a);
var r2 = getRank(b);
return r1>r2 ? 1 : r1<r2 ?-1: 0;
})
document.getElementById("result").innerHTML = JSON.stringify(jsonData1.ProductImages.map(function(a){return a.itemId}));
<pre id="result"></pre>
答案 2 :(得分:1)
我们的想法是为JS中的sort函数编写一个自己的比较函数: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
在此比较功能中,您还可以直接设置"checkBoxField"
值。 (可能不是最有效的方式)
这是您的问题的一个示例:
var checkedIds =[2,1,4];//sort order array
//json array
var jsonData1= {
"ProductImages": [
{ "checkBoxField": false, "itemId": 1, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 2, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 3, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 4, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 5, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 6, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 7, "rateValue":"$34" },
{ "checkBoxField": false, "itemId": 8,"rateValue":"$34" }
]
};
var arr = jsonData1["ProductImages"]
arr.sort(function compare(a, b) {
if (checkedIds.indexOf(a["itemId"]) >= 0) {
a["checkBoxField"] = true;
}
if (checkedIds.indexOf(b["itemId"]) >= 0) {
b["checkBoxField"] = true;
}
if (checkedIds.indexOf(a["itemId"]) >= 0 && checkedIds.indexOf(b["itemId"]) < 0) {
return -1;
}
if (checkedIds.indexOf(b["itemId"]) >= 0 && checkedIds.indexOf(a["itemId"]) < 0) {
return 1;
}
return checkedIds.indexOf(a["itemId"])-checkedIds.indexOf(b["itemId"]);
});
console.log(arr);
如一条评论中所述,您应该在checkedIds
数组中使用整数。
答案 3 :(得分:0)
这是一个无聊,非聪明但易于理解的迭代解决方案:
var sortMe = function(ids, data) {
var i,j,ret = [];
// first get the items with matched ids:
for (i=0; i<ids.length;i++) {
for (j=0; j<data.length; j++) {
if (data[j].itemId == ids[i]) {
ret.push(data[j]);
}
}
}
// now get everything else (in its original order):
for (i=0; i<data.length; i++) {
// note: ids[] contains strings, so need to add "" to each data.itemId:
if (ids.indexOf(data[i].itemId+"") == -1) {
ret.push(data[i]);
}
}
return ret;
};
var checkedIds = ["2", "1", "4"]; //sort order array
var jsonData1 = {
"ProductImages": [{
"checkBoxField": false,
"itemId": 1,
"rateValue": "$34"
}, {
"checkBoxField": false,
"itemId": 2,
"rateValue": "$34"
}, {
"checkBoxField": false,
"itemId": 3,
"rateValue": "$34"
}, {
"checkBoxField": false,
"itemId": 4,
"rateValue": "$34"
}, {
"checkBoxField": false,
"itemId": 5,
"rateValue": "$34"
}, {
"checkBoxField": false,
"itemId": 6,
"rateValue": "$34"
}, {
"checkBoxField": false,
"itemId": 7,
"rateValue": "$34"
}, {
"checkBoxField": false,
"itemId": 8,
"rateValue": "$34"
}]
};
var foo = sortMe(checkedIds, jsonData1.ProductImages);
console.log(foo);
答案 4 :(得分:-1)
您似乎想要一种selection sort区别,而不是寻找最小值,而是在列表中查找下一个值。