如何查找密钥在JSON对象或数组中出现的次数 - JavaScript

时间:2017-09-21 11:30:02

标签: javascript json

例如,我有一个像这样的JSON对象,如何找到密钥:"StateID"出现的次数?

[{"StateID":"42","State_name":"Badakhshan","CountryID":"1"},
{"StateID":"43","State_name":"Badgis","CountryID":"1"},
{"StateID":"44","State_name":"Baglan","CountryID":"1"},
{"StateID":"45","State_name":"Balkh","CountryID":"1"},
{"StateID":"46","State_name":"Bamiyan","CountryID":"1"},
{"StateID":"47","State_name":"Farah","CountryID":"1"},
{"StateID":"48","State_name":"Faryab","CountryID":"1"},
{"StateID":"49","State_name":"Gawr","CountryID":"1"},
{"StateID":"50","State_name":"Gazni","CountryID":"1"},
{"StateID":"51","State_name":"Herat","CountryID":"1"},
{"StateID":"52","State_name":"Hilmand","CountryID":"1"},
{"StateID":"53","State_name":"Jawzjan","CountryID":"1"},
{"StateID":"54","State_name":"Kabul","CountryID":"1"},
{"StateID":"55","State_name":"Kapisa","CountryID":"1"},
{"StateID":"56","State_name":"Khawst","CountryID":"1"},
{"StateID":"57","State_name":"Kunar","CountryID":"1"},
{"StateID":"58","State_name":"Lagman","CountryID":"1"},
{"StateID":"59","State_name":"Lawghar","CountryID":"1"},
{"StateID":"60","State_name":"Nangarhar","CountryID":"1"},
{"StateID":"61","State_name":"Nimruz","CountryID":"1"},
{"StateID":"62","State_name":"Nuristan","CountryID":"1"},
{"StateID":"63","State_name":"Paktika","CountryID":"1"},
{"StateID":"64","State_name":"Paktiya","CountryID":"1"},
{"StateID":"65","State_name":"Parwan","CountryID":"1"},
{"StateID":"66","State_name":"Qandahar","CountryID":"1"},
{"StateID":"67","State_name":"Qunduz","CountryID":"1"},
{"StateID":"68","State_name":"Samangan","CountryID":"1"},
{"StateID":"69","State_name":"Sar-e Pul","CountryID":"1"},
{"StateID":"70","State_name":"Takhar","CountryID":"1"},
{"StateID":"71","State_name":"Uruzgan","CountryID":"1"},
{"StateID":"72","State_name":"Wardag","CountryID":"1"},
{"StateID":"73","State_name":"Zabul","CountryID":"1"}]

5 个答案:

答案 0 :(得分:1)

这样的事情可能是:

var data = [your data here];
console.log('Count', data.filter(function(obj){ return Object.keys(obj).indexOf('StateID') !== -1; }).length);

答案 1 :(得分:1)

你可以用以下方式做到这一点



let arr =[{"StateID":"42","State_name":"Badakhshan","CountryID":"1"},{"StateID":"43","State_name":"Badgis","CountryID":"1"},{"StateID":"44","State_name":"Baglan","CountryID":"1"},{"StateID":"45","State_name":"Balkh","CountryID":"1"},{"StateID":"46","State_name":"Bamiyan","CountryID":"1"},{"StateID":"47","State_name":"Farah","CountryID":"1"},{"StateID":"48","State_name":"Faryab","CountryID":"1"},{"StateID":"49","State_name":"Gawr","CountryID":"1"},{"StateID":"50","State_name":"Gazni","CountryID":"1"},{"StateID":"51","State_name":"Herat","CountryID":"1"},{"StateID":"52","State_name":"Hilmand","CountryID":"1"},{"StateID":"53","State_name":"Jawzjan","CountryID":"1"},{"StateID":"54","State_name":"Kabul","CountryID":"1"},{"StateID":"55","State_name":"Kapisa","CountryID":"1"},{"StateID":"56","State_name":"Khawst","CountryID":"1"},{"StateID":"57","State_name":"Kunar","CountryID":"1"},{"StateID":"58","State_name":"Lagman","CountryID":"1"},{"StateID":"59","State_name":"Lawghar","CountryID":"1"},{"StateID":"60","State_name":"Nangarhar","CountryID":"1"},{"StateID":"61","State_name":"Nimruz","CountryID":"1"},{"StateID":"62","State_name":"Nuristan","CountryID":"1"},{"StateID":"63","State_name":"Paktika","CountryID":"1"},{"StateID":"64","State_name":"Paktiya","CountryID":"1"},{"StateID":"65","State_name":"Parwan","CountryID":"1"},{"StateID":"66","State_name":"Qandahar","CountryID":"1"},{"StateID":"67","State_name":"Qunduz","CountryID":"1"},{"StateID":"68","State_name":"Samangan","CountryID":"1"},{"StateID":"69","State_name":"Sar-e Pul","CountryID":"1"},{"StateID":"70","State_name":"Takhar","CountryID":"1"},{"StateID":"71","State_name":"Uruzgan","CountryID":"1"},{"StateID":"72","State_name":"Wardag","CountryID":"1"},{"StateID":"73","State_name":"Zabul","CountryID":"1"}]







let result = arr.reduce((a,b) => {
    a[b.StateID] = a[b.StateID] || 0;
    a[b.StateID]++;
    return a;
}, {});

console.log(result);




答案 2 :(得分:1)

您可以使用filter创建一个数组,其中的项目具有您感兴趣的属性并返回长度。



const
  source = [ {"StateID":"42","State_name":"Badakhshan","CountryID":"1"}, {"StateID":"43","State_name":"Badgis","CountryID":"1"}, {"StateID":"44","State_name":"Baglan","CountryID":"1"}, {"StateID":"45","State_name":"Balkh","CountryID":"1"}, {"StateID":"46","State_name":"Bamiyan","CountryID":"1"}, {"StateID":"47","State_name":"Farah","CountryID":"1"}, {"StateID":"48","State_name":"Faryab","CountryID":"1"}, {"StateID":"49","State_name":"Gawr","CountryID":"1"}, {"StateID":"50","State_name":"Gazni","CountryID":"1"}, {"StateID":"51","State_name":"Herat","CountryID":"1"}, {"StateID":"52","State_name":"Hilmand","CountryID":"1"}, {"StateID":"53","State_name":"Jawzjan","CountryID":"1"}, {"StateID":"54","State_name":"Kabul","CountryID":"1"}, {"StateID":"55","State_name":"Kapisa","CountryID":"1"}, {"StateID":"56","State_name":"Khawst","CountryID":"1"}, {"StateID":"57","State_name":"Kunar","CountryID":"1"}, {"StateID":"58","State_name":"Lagman","CountryID":"1"}, {"StateID":"59","State_name":"Lawghar","CountryID":"1"}, {"StateID":"60","State_name":"Nangarhar","CountryID":"1"}, {"StateID":"61","State_name":"Nimruz","CountryID":"1"}, {"StateID":"62","State_name":"Nuristan","CountryID":"1"}, {"StateID":"63","State_name":"Paktika","CountryID":"1"}, {"StateID":"64","State_name":"Paktiya","CountryID":"1"}, {"StateID":"65","State_name":"Parwan","CountryID":"1"}, {"StateID":"66","State_name":"Qandahar","CountryID":"1"}, {"StateID":"67","State_name":"Qunduz","CountryID":"1"}, {"StateID":"68","State_name":"Samangan","CountryID":"1"}, {"StateID":"69","State_name":"Sar-e Pul","CountryID":"1"}, {"StateID":"70","State_name":"Takhar","CountryID":"1"}, {"StateID":"71","State_name":"Uruzgan","CountryID":"1"}, {"StateID":"72","State_name":"Wardag","CountryID":"1"}, {"StateID":"73","State_name":"Zabul","CountryID":"1"} ];
  
/**
 * Returns the number of objects in the array that have a specific property.
 *
 * @param {Array} array          The array whose items to check for the 
 *                               existence of a property.
 * @param {String} propertyName  The name of the property the check for on 
 *                               the objects in the array.
 *
 * @returns {Number} The method returns the number of objects in the array 
 *                   that have the specified property.
 */
function countPropertyOccurence(array, propertyName) {
  const
    matchingItems = array.filter(item => item[propertyName] !== undefined);
    
  return matchingItems.length;
}

console.log(`Number of times "StateID" occurs: ${countPropertyOccurence(source, 'StateID')}`);




答案 3 :(得分:1)

var a = 0;
for (var i=0; i < obj.length; i++) {
if (obj[i].stateId) {
a++;
}
}
console.log("count a",a);

答案 4 :(得分:0)

十几种方法中的一种,请参阅其他解决方案的其他答案。

如果StateID存在且大于0,则使用Array.prototype.forEach循环进行检查。

obj = [{"StateID":"42","State_name":"Badakhshan","CountryID":"1"},
{"StateID":"43","State_name":"Badgis","CountryID":"1"},
{"StateID":"44","State_name":"Baglan","CountryID":"1"},
{"StateID":"45","State_name":"Balkh","CountryID":"1"},
{"StateID":"46","State_name":"Bamiyan","CountryID":"1"},
{"StateID":"47","State_name":"Farah","CountryID":"1"},
{"StateID":"48","State_name":"Faryab","CountryID":"1"},
{"StateID":"49","State_name":"Gawr","CountryID":"1"},
{"StateID":"50","State_name":"Gazni","CountryID":"1"},
{"StateID":"51","State_name":"Herat","CountryID":"1"},
{"StateID":"52","State_name":"Hilmand","CountryID":"1"},
{"StateID":"53","State_name":"Jawzjan","CountryID":"1"},
{"StateID":"54","State_name":"Kabul","CountryID":"1"},
{"StateID":"55","State_name":"Kapisa","CountryID":"1"},
{"StateID":"56","State_name":"Khawst","CountryID":"1"},
{"StateID":"57","State_name":"Kunar","CountryID":"1"},
{"StateID":"58","State_name":"Lagman","CountryID":"1"},
{"StateID":"59","State_name":"Lawghar","CountryID":"1"},
{"StateID":"60","State_name":"Nangarhar","CountryID":"1"},
{"StateID":"61","State_name":"Nimruz","CountryID":"1"},
{"StateID":"62","State_name":"Nuristan","CountryID":"1"},
{"StateID":"63","State_name":"Paktika","CountryID":"1"},
{"StateID":"64","State_name":"Paktiya","CountryID":"1"},
{"StateID":"65","State_name":"Parwan","CountryID":"1"},
{"StateID":"66","State_name":"Qandahar","CountryID":"1"},
{"StateID":"67","State_name":"Qunduz","CountryID":"1"},
{"StateID":"68","State_name":"Samangan","CountryID":"1"},
{"StateID":"69","State_name":"Sar-e Pul","CountryID":"1"},
{"StateID":"70","State_name":"Takhar","CountryID":"1"},
{"StateID":"71","State_name":"Uruzgan","CountryID":"1"},
{"StateID":"72","State_name":"Wardag","CountryID":"1"},
{"StateID":"73","State_name":"Zabul","CountryID":"1"}];

var count = 0;
obj.forEach(function(value){
    value.StateID > 0 ? count++ : null;
});
console.log(count);