Return Maximum value from associative array (object)

I have an associative array that looks like this:

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, since this field is inside the object of the object, it is a little more complicated than just following a similar method with something described.

Passing an object through the object and saving the value, and then replacing it, if the next size is larger, looks like the simplest and most obvious option.

I am just asking this question if there is a simpler (smarter) way to achieve this other than the method described above?

+5
source share
2 answers

... , , - Object.keys Array.prototype.map, Math.max, :

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'];
                        }));
+11

(for) . - , Underscore.js, .

0

All Articles