如何从JSON对象中检索具有最高数值的多组数据?

时间:2016-03-10 07:39:12

标签: javascript json

如何使用最高值获取以下数据中的所有车辆?

我有一个JSON对象,如下所示:

var data = [{
    "Make": "Honda",
    "Model": "Civic",
    "Year": "2005",
    "Color": "Red",
    "Status": "In-Production",
    "Version": 1
}, {
    "Make": "Honda",
    "Model": "Civic",
    "Year": "2005",
    "Color": "Red",
    "Status": "Discontinued",
    "Version": 2
},{
    "Make": "Toyota",
    "Model": "Camry",
    "Year": "2016",
    "Color": "Black",
    "Status": "In-Production",
    "Version": 5
}, {
    "Make": "Toyota",
    "Model": "Camry",
    "Year": "2016",
    "Color": "Black",
    "Status": "Discontinued",
    "Version": 6
}];

在上面的JSON object中,对于每个Vehicle Make,我需要获得最高版本。

因此结果将是丰田版本6和本田版本2版本的所有值。

结果:

[{
        "Make": "Honda",
        "Model": "Civic",
        "Year": "2005",
        "Color": "Red",
        "Status": "Discontinued",
        "Version": 2
    },{
        "Make": "Toyota",
        "Model": "Camry",
        "Year": "2016",
        "Color": "Black",
        "Status": "Discontinued",
        "Version": 6
    }]

到目前为止,我只能获得最高价值而不是多辆车:

例如:这将给我版本8但不是8和2。

var query = data.filter(function(result){

    var maxVersion = [];

    for ( var j = 0; j < data.length; j++ ){
        maxVersion.push(data[j].Version);
    }

    return result.Version === Math.max.apply(null, maxVersion);

});

此问题的类似版本已得到解答,但我无法想出一个解决我的特定方案来获取多组数据的方法。 Getting max value(s) in JSON array

3 个答案:

答案 0 :(得分:2)

这是一个带有对象和Array#forEach()循环的解决方案。

&#13;
&#13;
var data = [{ "Make": "Honda", "Model": "Civic", "Year": "2005", "Color": "Red", "Status": "In-Production", "Version": 1 }, { "Make": "Honda", "Model": "Civic", "Year": "2005", "Color": "Red", "Status": "Discontinued", "Version": 2 }, { "Make": "Toyota", "Model": "Camry", "Year": "2016", "Color": "Black", "Status": "In-Production", "Version": 5 }, { "Make": "Toyota", "Model": "Camry", "Year": "2016", "Color": "Black", "Status": "Discontinued", "Version": 6 }],
    maxed = function (data) {
        var r = [], o = {};
        data.forEach(function (a) {
            if (!(a.Make in o)) {
                o[a.Make] = r.push(a) - 1;
                return;
            }
            if (a.Version > r[o[a.Make]].Version) {
                r[o[a.Make]] = a;
            }
        });
        return r;
    }(data);

document.write('<pre>' + JSON.stringify(maxed, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个。先找到数据中所有独特的车辆。然后搜索每辆车的数据并比较版本。

&#13;
&#13;
var data = [{
  "Make": "Honda",
  "Model": "Civic",
  "Year": "2005",
  "Color": "Red",
  "Status": "In-Production",
  "Version": 1
}, {
  "Make": "Honda",
  "Model": "Civic",
  "Year": "2005",
  "Color": "Red",
  "Status": "Discontinued",
  "Version": 2
}, {
  "Make": "Toyota",
  "Model": "Camry",
  "Year": "2016",
  "Color": "Black",
  "Status": "In-Production",
  "Version": 5
}, {
  "Make": "Toyota",
  "Model": "Camry",
  "Year": "2016",
  "Color": "Black",
  "Status": "Discontinued",
  "Version": 6
}];



var makes = [];

for (i = 0; i < data.length; i++) {
  if (makes.indexOf(data[i].Make) == -1) {
    makes.push(data[i].Make)
  }
}



var vehicle = makes.map(function(m) {

  var res = data.filter(function(v) {
    return v.Make == m
  });

  var result = res.reduce(function(p, c) {
    return p.Version > c.Version ? p : c;
  })

  return result
})

document.write('<pre>' + JSON.stringify(vehicle, 0, 4) + '</pre>')
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这一直是我的一个非常常见的功能,现在很适合这个问题。

它只是循环遍历原始数组,创建一个永远在开发的对象,然后将结果推送到数组。

var arr = [
  { name: 'Tom', age: 12 },
  { name: 'John', age: 8 },
  { name: 'Tom', age: 2 },
  { name: 'Kelly', age: 6 },
  { name: 'Kelly', age: 10 }
];

var obj = {};
var oldest = [];

arr.forEach(function(entry, index){
  oldest = [];
  if(obj[entry.name] && obj[entry.name].age < entry.age) {
    obj[entry.name] = entry;
  } else if(!obj[entry.name]) {
    obj[entry.name] = entry;
  }
  for(key in obj){
    oldest.push(obj[key])
  }
})
console.log(oldest)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>