简体   繁体   English

数组中不同属性值的对象的Javascript数

[英]Javascript number of object by different property values in array

Best way to count the number of objects in a array with different object property p values. 计算具有不同对象属性p值的数组中对象数量的最佳方法。

function([{"p":"a"},{"p":"b"},{"p":"a"}]){
    // some code
}
// in this case return 2

You can use Array.prototype.filter() to keep values that you want. 您可以使用Array.prototype.filter()保留所需的值。 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. 在这种情况下,您必须创建一个变量temp来存储重复的值,并且在filter function内(如果不存在的话)返回true 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) 

With a Map : 带有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. 否则, Map是您的答案。

lodash is nice for things like this. lodash很适合这样的事情。

Use 采用

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

and it'll return 2 . 它会返回2

 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')); 

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. 例如Map()仅在最新的浏览器中可用,而在IE 10或更低版本中不可用。

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) . 这里一个复杂的部分是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" : 您可以只使用values.hasOwnProperty(value) ,但是如果您的属性值之一是字符串"hasOwnProperty" ,那将失败:

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

Using the longer form avoids this issue. 使用较长的格式可以避免此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM