当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Sort json data based on another array

问题描述:

I have one json array and other array for sort order.Now i want to sort the json array according to the sort array items.

 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" }

]

};

Output Should be

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" }

]

};

网友答案:

You can have a custom sort based on calculated rank:

JSFiddle.

 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>
网友答案:

The idea is to write an own compare function for the sort function in JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

In this compare function you can also set the "checkBoxField" value directly. (Might be not the most efficient way)

This is an example for your problem:

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);

As mentioned in one comment you should use integers in your checkedIds array.

https://jsfiddle.net/yxq0xL6L/1/

网友答案:

You could use a hash table for the order and a default value, big enough to move not prioritized values to the end.

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);
网友答案:

Here is a boring, non-clever, but easy-to-understand iterative solution:

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);
网友答案:

It seems you want a sort of selection sort with the distinction that instead of looking for a minimum value, you look for the next value in your list.

分享给朋友:
您可能感兴趣的文章:
随机阅读: