简体   繁体   English

使用d3或jQuery的json对象中有多少个不同的密钥对值

[英]how many different key pair values in a json object using d3 or jquery

I have an array of objects which looks somewhat like this 我有一些看起来像这样的对象

 [ {
      "created_date" : "2013-08-12T06:25:00",
      "descriptor" : "Noise, Barking Dog (NR5)"
    },
    {
      "created_date" : "2013-08-17T06:25:00",
      "descriptor" : "Noise, Barking Dog (NR5)"
    },
    {
      "created_date" : "2013-08-17T02:25:00",
      "descriptor" : "Noise, Barking Dog (NR5)"
    },
    {
      "created_date" : "2013-08-12T06:25:00",
      "descriptor" : "Loud Music"
    },
    {
      "created_date" : "2013-08-17T06:25:00",
      "descriptor" : "Loud Music"
    },
    {
      "created_date" : "2013-08-17T02:25:00",
      "descriptor" : "Construction Noise"
    },
    {
      "created_date" : "2013-08-17T02:25:00",
      "descriptor" : "Construction Noise"
    }]

The question is how do I count how many "descriptor" categories are there and their name How should I approach this using D3 or jquery. 问题是如何计算有多少“描述符”类别及其名称,我该如何使用D3或jquery进行处理。 Basically the idea is to form different groups 基本上,想法是形成不同的群体

You can use d3's d3.map() to map each descriptor to the number of its occurrences. 您可以使用d3的d3.map()将每个descriptor映射到其出现的次数。

var map = d3.map();
data.forEach(function(d) {
    var descriptorCount = map.get(d.descriptor);
    map.set(d.descriptor, descriptorCount === undefined ? 1 : ++descriptorCount);
});
console.log(map); // {Noise, Barking Dog (NR5): 3, Loud Music: 2, Construction Noise: 2}

Fiddle 小提琴

var UniqueDescriptors = "";
for (var i =0; i<array.length;i++) {
    var obj = array[i];
    if (obj.descriptor && UniqueDescriptors.indexOf(obj.descriptor) == -1) {
        UniqueDescriptors += obj.descriptor + '|';
    }
}
// this returns just the number
//return (UniqueDescriptors.split('|').length - 1);
// this returns an array of the different descriptors if you need the names. you also can count the number by accessing the length property
return UniqueDescriptors.split('|').pop();

of course the obvious flaw of this script is that it assumes there are no pipes in the descriptor values. 当然,此脚本的明显缺陷是它假定描述符值中没有管道。

Just iterate over the array and collect each descriptor: 只需遍历数组并收集每个描述符:

var descriptors = (function(data) {
    var seen = {};
    data.forEach(function(obj) {
        seen[obj.descriptor] = true;
    });
    return Object.keys(seen);
}(data));

To get the number of unique descriptors, just take the length of the array: 要获取唯一描述符的数量,只需获取数组的长度即可:

descriptors.length

If you need to support older browser that don't support Object.keys or Array#forEach : 如果您需要支持不支持Object.keysArray#forEach旧版浏览器:

var descriptors = (function(data) {
    var seen = {};
    var descriptors = [];
    for (var i = 0, l = data.length; i < l; i++) {
        var descriptor = data[i].descriptor;
        if (!seen[descriptor]) {
            descriptors.push(descriptor);
            seen[descriptor] = true;
        }
    }
    return descriptors;
}(data));

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

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