如何使用javascript计算属性在JSON数组中出现的次数?

如何使用javascript计算属性在JSON数组中出现的次数?,javascript,json,Javascript,Json,我从一个公共数据API中检索到一个JSON对象,它看起来类似于下面的内容 [ { "category": "Burglary", "location_type": "Force", "location": { "latitude": "51.497877", "street": { "id": 953834, "name": "On or near Major Road" }, "long

我从一个公共数据API中检索到一个JSON对象,它看起来类似于下面的内容

[
  {
    "category": "Burglary",
    "location_type": "Force",
    "location": {
      "latitude": "51.497877",
      "street": {
        "id": 953834,
        "name": "On or near Major Road"
      },
      "longitude": "-0.064175"
    },
    "context": "",
    "outcome_status": null,
    "persistent_id": "",
    "id": 53832838,
    "location_subtype": "",
    "month": "2016-12"
  },
  {
    "category": "anti-social-behaviour",
    "location_type": "Force",
    "location": {
      "latitude": "51.497877",
      "street": {
        "id": 953834,
        "name": "On or near Major Road"
      },
      "longitude": "-0.064175"
    },
    "context": "",
    "outcome_status": null,
    "persistent_id": "",
    "id": 53832841,
    "location_subtype": "",
    "month": "2016-12"
  },
  {
    "category": "anti-social-behaviour",
    "location_type": "Force",
    "location": {
      "latitude": "51.497877",
      "street": {
        "id": 953834,
        "name": "On or near Major Road"
      },
      "longitude": "-0.064175"
    },
    "context": "",
    "outcome_status": null,
    "persistent_id": "",
    "id": 53832849,
    "location_subtype": "",
    "month": "2016-12"
  },
  {
    "category": "anti-social-behaviour",
    "location_type": "Force",
    "location": {
      "latitude": "51.500440",
      "street": {
        "id": 953881,
        "name": "On or near Chambers Street"
      },
      "longitude": "-0.066891"
    },
    "context": "",
    "outcome_status": null,
    "persistent_id": "",
    "id": 53832881,
    "location_subtype": "",
    "month": "2016-12"
  } ]

我试图统计每一类中发生了多少起犯罪。以“obj[0].category”访问类别值是否正确?计算它们的最佳方法是什么?

假设您已经解析了JSON以获得一个对象数组,您可以使用来计算类别。reduce为数组中的每个项调用一次函数,将累加器acc(在本例中为对象{})和当前数组值作为参数传递

结果将是如下所示的对象:

{
  "Burglary": 1,
  "anti-social-behaviour": 3
}
…所以,如果你想知道发生了多少起入室盗窃案,你可以说是类别[入室盗窃]

展开并运行以下代码段以查看其工作情况

风险值数据=[ { 类别:爆窃,, 位置类型:力, 地点:{ 纬度:51.497877, 街道:{ 身份证号码:953834, 名称:主要道路上或附近 }, 经度:-0.064175 }, 上下文:, 结果状态:空, 持久性用户id:, 身份证号码:53832838, 位置\子类型:, 月份:2016-12 }, { 类别:反社会行为, 位置类型:力, 地点:{ 纬度:51.497877, 街道:{ 身份证号码:953834, 名称:主要道路上或附近 }, 经度:-0.064175 }, 上下文:, 结果状态:空, 持久性用户id:, 身份证号码:53832841, 位置\子类型:, 月份:2016-12 }, { 类别:反社会行为, 位置类型:力, 地点:{ 纬度:51.497877, 街道:{ 身份证号码:953834, 名称:主要道路上或附近 }, 经度:-0.064175 }, 上下文:, 结果状态:空, 持久性用户id:, 身份证号码:53832849, 位置\子类型:, 月份:2016-12 }, { 类别:反社会行为, 位置类型:力, 地点:{ 纬度:51.500440, 街道:{ 身份证号码:953881, 姓名:在钱伯斯街或附近 }, 经度:-0.066891 }, 上下文:, 结果状态:空, 持久性用户id:, 身份证号码:53832881, 位置\子类型:, 月份:2016-12 } ] var类别=数据。还原函数ACC,犯罪{ if!acc[犯罪类别] acc[犯罪类别]=1; 其他的 acc[犯罪类别]++; 返回acc; }, {}; console.logcategories; 控制台.日志类别[盗窃] 另一种方法是使用ArrayforEach

var json=[{类别:入室盗窃,地点类型:暴力,地点:{纬度:51.497877,街道:{id:953834,姓名:在主要道路上或附近},经度:-0.064175},上下文:,结果状态:空,持续性{id:,id:53832838,地点子类型:,月份:2016-12},{类别:反社会行为,地点类型:暴力,地点:{纬度:51.497877,街道:{id:953834,姓名:主要道路上或附近},经度:-0.064175},上下文:,结果状态:空,持续性{id:,id:53832841,位置子类型:,月份:2016-12},{类别:反社会行为,位置类型:力,位置:{纬度:51.497877,街道:{id:953834,姓名:主要道路上或附近},经度:-0.064175},上下文:,结果状态:空,持续性,id:53832849,位置子类型:,月份:2016-12},{类别:反社会行为,位置类型:力量,位置:{纬度:51.500440,街道:{id:953881,姓名:在钱伯斯街上或附近},经度:-0.066891},上下文:,结果状态:空,持续性,id:53832881,位置子类型:,月份:2016-12}], 结果={}; json.forEachfunctionv{ !result[v.category]?result[v.category]=1:result[v.category]+=1; }; console.logresult;使用Array.prototype.reduce函数:

var数据=[{类别:入室盗窃,地点类型:暴力,地点:{纬度:51.497877,街道:{id:953834,姓名:在主要道路上或附近},经度:-0.064175},背景:,结果状态:空,持续性{id:,id:53832838,地点子类型:,月份:2016-12},{类别:反社会行为,地点类型:暴力,地点:{纬度:51.497877,街道:{id:953834,姓名:主要道路上或附近},经度:-0.064175},上下文:,结果状态:空,持续性{id:,id:53832841,位置子类型:,月份:2016-12},{类别:反社会行为,位置类型:力,位置:{纬度:51.497877,街道:{id:953834,姓名:主要道路上或附近},经度:-0.064175},上下文:,结果状态:空,持续性,id:53832849,位置子类型:,月份:2016-12},{类别:反社会行为,位置类型:力量,位置:{纬度:51.500440,街道:{id:953881,姓名:在钱伯斯街上或附近},经度:-0.066891},上下文:,结果状态:空,持续性,id:53832881,位置子类型:,月份:2016-12}], 计数=数据。还原函数r,o{ r[o.category]?r[o.category]++:r[o.category]=1; 返回r; }, {}; console.logcounts;您可以使用


循环并跟踪每个类别并增加它们。。。
{
  "Burglary": 1,
  "anti-social-behaviour": 3
}
var arr = [{
  "category": "Burglary",
}, {
  "category": "anti-social-behaviour",
}, {
  "category": "anti-social-behaviour",
}, {
  "category": "anti-social-behaviour",
}];

var results = arr.reduce(function(result, itm){
    return (result[itm.category] = (result[itm.category] || 0) + 1, result);
}, {});

console.log(results); //Object {Burglary: 1, anti-social-behaviour: 3}