0

Best way to count the number of objects in a array with different object property p values.

function([{"p":"a"},{"p":"b"},{"p":"a"}]){
    // some code
}
// in this case return 2
1
  • A clue: .map().filter().length or reduce() <= (disclaimer, my blog post) Commented Jul 2, 2015 at 6:07

5 Answers 5

1

You can use Array.prototype.filter() to keep values that you want. In this case, you must create a variable temp for storing duplicate values and also within the filter function returns true if it does not exist in case. So you have a new array of unique values.

var arr = [{"p":"a"},{"p":"b"},{"p":"a"}], temp = [];

var arrUniques = arr.filter(function(obj){
  return temp.indexOf(obj.p) === -1 ? !!temp.push(obj.p) : false
});

alert(arrUniques.length)

Sign up to request clarification or add additional context in comments.

Comments

0

With a Map:

var props = new Map();
for (var i = 0; i < array.length; i++) {
    var prop = array[i].p,
        count = props.get(prop) || 0;

    props.set(prop, count + 1);
}

var size = props.size;

If your properties can be safely casted to strings, you can use a common object:

var props = {};
...
var size = Object.keys(props).length;

Otherwise, Map is your answer.

Comments

0

function getUniquePropertyCount(a, p) {
    return a.reduce(function (res, el) {
        !~res.indexOf(el[p]) && res.push(el[p]);
        return res;
    }, []).length;
}
document.write(getUniquePropertyCount([{ "p": "a" }, { "p": "b" }, { "p": "a" }], 'p'));

Comments

0

I suppose this is one of those questions where if you ask four programmers, you'll get five answers.

The other answers so far show some interesting approaches. I would watch out for browser compatibility; for example Map() is only available in the newest browsers, not in IE 10 or prior.

So here's one more solution. It's a bit more code, but it's pretty easy to understand, and it works in every browser:

function countUniqueProperties( key, array ) {
    var count = 0, values = {};
    for( var i = 0;  i < array.length;  ++i ) {
        var value = array[i][key];
        if( ! Object.prototype.hasOwnProperty.call( values, value) ) {
            ++count;
            values[value] = true;
        }
    }
    return count;
}

countUniqueProperties( 'p', [ {p:'a'}, {p:'b'}, {p:'a'} ] );

The one complicated part here is the Object.prototype.hasOwnProperty.call(values,value). You could just use values.hasOwnProperty(value), but that would fail if one of your property values was the string "hasOwnProperty":

countUniqueProperties( 'p', [ {p:'a'}, {p:'hasOwnProperty'}, {p:'a'} ] );

Using the longer form avoids this issue.

Comments

0

lodash is nice for things like this.

Use

_.uniq([{"p":"a"},{"p":"b"},{"p":"a"}]).length

and it'll return 2.

Comments

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.