2

How do I get all of the vehicle make in the data below with the highest values?

I have a JSON object that looks like this:

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
}];

In the above JSON object, for each Vehicle Make, I need to get the highest version.

So the result will be all the values for Version 6 for Toyota and Version for 2 for Honda.

Result:

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

So far I am only able to get the highest value but not multiple vehicles:

For example: This will give me Version 8 but not 8 and 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);

});

A similar version to this question has been answered but I cannot come up with a solution to my particular scenario to get multiple sets of data. Getting max value(s) in JSON array.

Community
  • 1
  • 1
Asynchronous
  • 3,917
  • 19
  • 62
  • 96

3 Answers3

2

This is a solution with an object and an Array#forEach() loop.

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>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

try this.first find all the unique vehicles in the data. then search the data for each vehicle and the compare the versions.

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>')
Pavan Teja
  • 3,192
  • 1
  • 15
  • 22
0

This has been a pretty common function of mine for awhile now and applies to this problem nicely.

It simply loops over the original array, creates an ever developing object and then pushes the results to an array.

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>
8eecf0d2
  • 1,569
  • 1
  • 13
  • 23