具有相同键的阵列中有多少个对象

时间:2016-07-09 15:19:34

标签: javascript

我想根据参数计算数组中有多少个对象具有相同的 name 属性。我怎样才能做到这一点?

实际上这是我的阵列:

[  
   {  
      "name":"aaa",
      "id":"2100",
      "designation":"developer"
   },
   {  
      "name":"bbb",
      "id":"8888",
      "designation":"team lead"
   },
   {  
      "name":"ccc",
      "id":"6745",
      "designation":"manager"
   },
   {  
      "name":"aaa",
      "id":"9899",
      "designation":"sw"
   }
]

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:0)

Object没有length属性。仍然可以使用Object.keys返回给定对象自己的可枚举属性的数组。然后你可以使用length属性。

var a = { "name":"bbb", "id":"8888", "designation":"team lead" }
var m = Object.keys(a).length;

JSFIDDLE

  

如果重复意味着,我希望得到ii的长度为2

你的意思是用重复的键说对象吗?

答案 1 :(得分:0)

您可以迭代数组的元素并检查每个属性并使用哈希表进行计数。

var data = [{ "name": "aaa", "id": "2100", "designation": "developer" }, { "name": "bbb", "id": "8888", "designation": "team lead" }, { "name": "ccc", "id": "6745", "designation": "manager" }, { "name": "aaa", "id": "9899", "designation": "sw" }],
    max = 0;

data.forEach(function (a) {
    Object.keys(a).forEach(function (k) {
        var key = k + '|' + a[k];
        this[key] = (this[key] || 0) + 1;
        if (this[key] > max) {
            max = this[key];
        }
    }, this);
}, {});

console.log(max);

答案 2 :(得分:0)

要计算有多少个对象具有相同的,您可以这样做:



var array = [  
   {  
      "name":"aaa",
      "id":"2100",
      "designation":"developer"
   },
   {  
      "name":"bbb",
      "id":"8888",
      "designation":"team lead"
   },
   {  
      "name":"ccc",
      "id":"6745",
      "designation":"manager"
   },
   {  
      "name":"aaa",
      "id":"9899",
      "designation":"sw"
   }
];

function countDuplicates(array) {
  var count = {};
  array.forEach(function(element) {
    count[element.name] = (count[element.name] || 0) + 1;
  });
  return count;
}

console.log(countDuplicates(array));




注意:这是一般方法,不仅可以通过唯一键计算所有项目。

但是,如果您只想使用" aaa"来获取多少个对象,例如,您只需传递第二个。函数的参数,如下所示:



var array = [  
   {  
      "name":"aaa",
      "id":"2100",
      "designation":"developer"
   },
   {  
      "name":"bbb",
      "id":"8888",
      "designation":"team lead"
   },
   {  
      "name":"ccc",
      "id":"6745",
      "designation":"manager"
   },
   {  
      "name":"aaa",
      "id":"9899",
      "designation":"sw"
   }
];

function countDuplicates(array, key) {
  var count = {};
  array.forEach(function(element) {
    if (element.name == key) { 
      count[element.name] = (count[element.name] || 0) + 1;
    }
  });
  return count;
}

console.log(countDuplicates(array, "aaa"));




您还可以使用本机方法Array.prototype.filter()返回对象/计数器:



var array = [  
   {  
      "name":"aaa",
      "id":"2100",
      "designation":"developer"
   },
   {  
      "name":"bbb",
      "id":"8888",
      "designation":"team lead"
   },
   {  
      "name":"ccc",
      "id":"6745",
      "designation":"manager"
   },
   {  
      "name":"aaa",
      "id":"9899",
      "designation":"sw"
   }
];

function countDuplicates(array, key) {
  var filterArray = array.filter(function(element) {
    return element.name == key;
  })
  return filterArray.length;
}

console.log(countDuplicates(array, "aaa"));




我希望它有所帮助。

答案 3 :(得分:0)

所以你有你的数据

var myData =[
    {
        "name":"aaa",
        "id":"2100",
        "designation":"developer"
    },
    {
        "name":"bbb",
        "id":"8888",
        "designation":"team lead"
    },
    {
        "name":"ccc",
        "id":"6745",
        "designation":"manager"
    },
    {
        "name":"aaa",
        "id":"9899",
        "designation":"sw"
    }
];

现在让我们做一个有用的基本功能

// A function to get all items with a spesific value on a spesific attribute
var getByAttribute = function(attribute, value, data){
    // Results is an array
    var results = [];

    // For each item
    for(var i=0; i<data.length; i++){

        // Check if attribute match
        if(data[i][attribute] == value)

            // If it match, insert in on results
            results.push(data[i]);
    }

    return results;
};

现在我们将使用上面的函数来创建更多

// A function to get all items based on a name
var getByName = function(name, data){
    return getByAttribute("name", name, data);
};

// A function to get all items based on an id
var getById = function(id, data){
    return getByAttribute("id", id, data);
};

// A function to get all items based on a designation
var getByDesignation = function(designation, data){
    return getByAttribute("designation", designation, data);
};

所以现在我们可以算一下名为“aaa”的项目 使用代码

var count = getByName("aaa", myData).length;
/*
    getByName("aaa", myData) retrurns 

    [
        {
            "name":"aaa",
            "id":"2100",
            "designation":"developer"
        },
        {
            "name":"aaa",
            "id":"9899",
            "designation":"sw"
        }
    ]

    so if we use on it .length
    we get 2
*/