I have an associative array which looks like:
var data = {
0: {
'Number_of_Something': 212
},
1: {
'Number_of_Something': 65
},
2: {
'Number_of_Something': 657
}
}
I need to extract the highest value in the field Number_of_Something
, however, because it is a field within an object of an object, it is a little more complicated than just following a similar method to something outlined here.
Looping through the object and storing the value, then replacing it if the next one is larger seems like the easiest and obvious option.
I am simply asking this question in case there is a simpler (smarter) way of achieving this other than the method outlined above?
So the limit is (2^32)-1 or 4294967295 if adhering to the spec.
Yes, an array is legal as top-level JSON-text.
simpler can be subjective... Another way to achieve what you ask is to get an array of the values using Object.keys and Array.prototype.map, and use the other solution with Math.max that you linked :
var data = {
0: {
'Number_of_Something': 212
},
1: {
'Number_of_Something': 65
},
2: {
'Number_of_Something': 657
}
}
var max = Math.max.apply(null,
Object.keys(data).map(function(e) {
return data[e]['Number_of_Something'];
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With