降序格式化JSON

时间:2019-02-14 05:48:16

标签: javascript json sorting

在下面的json文件中,我想按降序对docs字段中的内容进行排序。有两个街区。数据是动态的,意味着在文档中可以有多个块-可以是5个,也可以是10个这样的块。

{
"Scoring":1.68466776,
"_text_":[
],
"id":"health_analytics.pdf",
"jac_Score":0.07370184254606366,
"matchScore":0.65,
"scaledTf_idf":0.0,
"searchTerm":[
"Machine learning",
"Deep learning",
"R",
"Python",
"Sas"
],
"sortingScore":59.78
}

{
    "Scoring":2.029842275,
    "_text_":[
    ],
    "id":"customer_channel_analytics_bang.pdf",
    "jac_Score":0.09041591320072333,
    "matchScore":0.6875,
    "scaledTf_idf":0.0,
    "searchTerm":[
    "Machine learning",
    "Deep learning",
    "Sas",
    "R"
    ],
    "sortingScore":44.03
    },

我想使用Sortingscoring(文档中的字段之一)对它进行排序。应该对文档中的整个块(在这种情况下有两个)进行排序,因此排序后的最终json应该如下所示。我已经提到了一些stackoverflow链接 sort json object in javascript 但无法获得。

{
"statusCode":200,
"body":{
"docs":[
{
"Scoring":2.029842275,
"_text_":[
],
"id":"customer_channel_analytics_bang.pdf",
"jac_Score":0.09041591320072333,
"matchScore":0.6875,
"scaledTf_idf":0.0,
"searchTerm":[
"Machine learning",
"Deep learning",
"Sas",
"R"
],
"sortingScore":44.03
},
{
"Scoring":1.68466776,
"_text_":[
],
"id":"health_analytics.pdf",
"jac_Score":0.07370184254606366,
"matchScore":0.65,
"scaledTf_idf":0.0,
"searchTerm":[
"Machine learning",
"Deep learning",
"R",
"Python",
"Sas"
],
"sortingScore":59.78
}
]
}
}

2 个答案:

答案 0 :(得分:0)

尝试这个

var a = {
      "Scoring":2.029842275,
      "_text_":[
      ],
      "id":"customer_channel_analytics_bang.pdf",
      "jac_Score":0.09041591320072333,
      "matchScore":0.6875,
      "scaledTf_idf":0.0,
      "searchTerm":[
      "Machine learning",
      "Deep learning",
      "Sas",
      "R"
      ],
      "sortingScore":44.03
  }
var b = {
      "Scoring":1.68466776,
      "_text_":[
      ],
      "id":"health_analytics.pdf",
      "jac_Score":0.07370184254606366,
      "matchScore":0.65,
      "scaledTf_idf":0.0,
      "searchTerm":[
      "Machine learning",
      "Deep learning",
      "R",
      "Python",
      "Sas"
      ],
      "sortingScore":59.78
}
var c = [ {a},{b}]
console.log(a.id)
console.log(b.id)
console.log(c.sort(function(a, b){
    return a - b;
}));

答案 1 :(得分:0)

var json = `{
  "statusCode": 200,
  "body": {
    "docs": [
      {
        "Scoring": 2.029842275,
        "_text_": [],
        "id": "customer_channel_analytics_bang.pdf",
        "jac_Score": 0.09041591320072333,
        "matchScore": 0.6875,
        "scaledTf_idf": 0,
        "searchTerm": [
          "Machine learning",
          "Deep learning",
          "Sas",
          "R"
        ],
        "sortingScore": 64.03
      },
      {
        "Scoring": 1.68466776,
        "_text_": [],
        "id": "health_analytics.pdf",
        "jac_Score": 0.07370184254606366,
        "matchScore": 0.65,
        "scaledTf_idf": 0,
        "searchTerm": [
          "Machine learning",
          "Deep learning",
          "R",
          "Python",
          "Sas"
        ],
        "sortingScore": 59.78
      },
      {
        "Scoring": 3.029842275,
        "_text_": [],
        "id": "sex_anal_lyctics.pdf",
        "jac_Score": 0.09041591320072333,
        "matchScore": 0.6875,
        "scaledTf_idf": 0,
        "searchTerm": [
          "Machine learning",
          "Deep learning",
          "Sas",
          "R"
        ],
        "sortingScore": 14.03
      }
    ]
  }
}`;



var obj = JSON.parse(json);

for(var i = 0; i < obj.body.docs.length; i++){
  obj.body.docs.forEach(function(value,index){
    var counter = index;
    var temp;
    if(index < obj.body.docs.length-1){
      if(obj.body.docs[index].sortingScore > obj.body.docs[counter+1].sortingScore){
          temp = obj.body.docs[index];
          obj.body.docs[index] = obj.body.docs[counter+1];
          obj.body.docs[counter+1] = temp;
      }
    }
  });
}

console.log(obj.body.docs);

//嗨,这是一个连续的记录,如果数据量很大,我不建议您将其更改为>或<以适合您的排序首选项

View In Fiddle