1

Say we have an array:

var antibiotics = [{
    bacteria: "Mycobacterium tuberculosis",
    penicillin: 800,
    streptomycin: 5,
    neomycin: 2,
    gram: "negative"
}, {
    bacteria: "Salmonella schottmuelleri",
    penicillin: 10,
    streptomycin: 0.8,
    neomycin: 0.09,
    gram: "negative"
}, {
    bacteria: "Proteus vulgaris",
    penicillin: 3,
    streptomycin: 0.1,
    neomycin: 0.1,
    gram: "negative"
}, {
    bacteria: "Klebsiella pneumoniae",
    penicillin: 850,
    gram: "negative"
}];

And we want to find minand max of all numerical properties of objects in array (penicillin, streptomycin and neomycin here) assuming values can be null/absent.

How to aggregate such data from an array of objects in JavaScript?

0

3 Answers 3

3

You could use Array.prototype.map() to pluck the values you require, and then pass as arguments to Math.max() or Math.min().

Math.max.apply(Math, values);
Sign up to request clarification or add additional context in comments.

2 Comments

don't forget that Math.max.apply will crap out and return NaN if any of the values in the source array are undefined.
Coulde you eveluete on map() a bit more: how to return more than one value from function we provide to map?
1

Unfortunately, JS standard library doesn't provide Array.max or a "pluck" (=collect) function, but there are many libraries that do, e.g. underscore:

maxPenicillin = _.max(_(antibiotics).pluck('penicillin')))

If you don't like libraries, these function are easy:

Array.prototype.max = function() {
    return this.reduce(function(a, b) {
        return a > b ? a : b;
    })
}

Array.prototype.pluck = function(prop) {
    return this.map(function(x) {
        return x[prop];
    })
}

maxPenicillin = antibiotics.pluck('penicillin').max()

but really, why would you want to reinvent the wheel? Just use a library.

Upd: if I interpret your comment correctly, you're looking for something like this:

var values = {};

_.each(antibiotics, function(x) {
    _.each(x, function(v, k) {
        if(!isNaN(v))
            values[k] = (values[k] || []).concat([v]);
    })
})

var minmax = {};

_.each(values, function(v, k) {
    minmax[k] = [_.min(v), _.max(v)]
})

Result:

{"penicillin":[3,850],"streptomycin":[0.1,5],"neomycin":[0.09,2]}

2 Comments

yeah... here problem was to grab all object's (!isNaN(object[prop])) so to have a degree of freedom from property names, and also have all properties values agregated, not just one.
Point was to get over all statistic - min and max from all Object numeric properties - here [0.09, 850] (as if thay were of same value type representable on chart)
0

This should do the trick.

http://jsfiddle.net/vgW4v/

Array.prototype.max = function() {
    return Math.max.apply(null, this);
};

Array.prototype.min = function() {
    return Math.min.apply(null, this);
};

And arrays are populated with values if they exist and simply calculated.

Hope this helps.

1 Comment

Good day, Point was to get over all statistic - min and max from all Object numeric properties - here [0.09, 850] (as if thay were of same value type representable on chart)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.