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.