Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中JSON文件中键值对实例的计数_Javascript_Json - Fatal编程技术网

JavaScript中JSON文件中键值对实例的计数

JavaScript中JSON文件中键值对实例的计数,javascript,json,Javascript,Json,我正在处理一个包含许多属性的大型对象数据集(见下文),并试图计算每个“类型”属性的实例数。最困难的是有大量不同的类型,因此我不能从预定义的列表开始。 因此,以下数据的理想输出为: "Medic Response" : 2 "Aid Response" : 1 提前谢谢 反对 address: "9001 Lake City Way Ne" datetime : "2011-02-12T18:04:00.000" incident_number : "F110013072" latitude :

我正在处理一个包含许多属性的大型对象数据集(见下文),并试图计算每个“类型”属性的实例数。最困难的是有大量不同的类型,因此我不能从预定义的列表开始。

因此,以下数据的理想输出为:

"Medic Response" : 2
"Aid Response" : 1
提前谢谢

反对

address: "9001 Lake City Way Ne"
datetime : "2011-02-12T18:04:00.000"
incident_number : "F110013072"
latitude :"47.693931"
longitude : "-122.30552"
type : "Medic Response"
address : "97 S Main St"
datetime : "2011-02-16T18:46:00.000"
incident_number : "F110014403"
latitude : "47.600044"
longitude : "-122.334219"
type : "Aid Response"
address : "509 3rd Av"
datetime : "2011-02-12T07:30:00.000"
incident_number : "F110012897"
latitude : "47.602114"
longitude : "-122.330809"
report_location :
type : "Medic Response"
反对

address: "9001 Lake City Way Ne"
datetime : "2011-02-12T18:04:00.000"
incident_number : "F110013072"
latitude :"47.693931"
longitude : "-122.30552"
type : "Medic Response"
address : "97 S Main St"
datetime : "2011-02-16T18:46:00.000"
incident_number : "F110014403"
latitude : "47.600044"
longitude : "-122.334219"
type : "Aid Response"
address : "509 3rd Av"
datetime : "2011-02-12T07:30:00.000"
incident_number : "F110012897"
latitude : "47.602114"
longitude : "-122.330809"
report_location :
type : "Medic Response"
反对

address: "9001 Lake City Way Ne"
datetime : "2011-02-12T18:04:00.000"
incident_number : "F110013072"
latitude :"47.693931"
longitude : "-122.30552"
type : "Medic Response"
address : "97 S Main St"
datetime : "2011-02-16T18:46:00.000"
incident_number : "F110014403"
latitude : "47.600044"
longitude : "-122.334219"
type : "Aid Response"
address : "509 3rd Av"
datetime : "2011-02-12T07:30:00.000"
incident_number : "F110012897"
latitude : "47.602114"
longitude : "-122.330809"
report_location :
type : "Medic Response"

您可以使用类型名称/计数的键/值散列,然后在对象集合上迭代,每次递增计数。例如:

var counts = {};
var objects = [/*array of your objects*/];

objects.forEach(function (o) {
    // add the type to the hash if it is missing;
    // set initial count to 0
    if (!counts.hasOwnProperty(o.type)) {
        counts[o.type] = 0;
    }
    // increment the count based on the type
    counts[o.type] += 1;
});

console.log(counts);

您可以使用类型名称/计数的键/值散列,然后在对象集合上迭代,每次递增计数。例如:

var counts = {};
var objects = [/*array of your objects*/];

objects.forEach(function (o) {
    // add the type to the hash if it is missing;
    // set initial count to 0
    if (!counts.hasOwnProperty(o.type)) {
        counts[o.type] = 0;
    }
    // increment the count based on the type
    counts[o.type] += 1;
});

console.log(counts);

您可以使用
array.reduce
将数据数组缩减为对象映射

这是假设它是一个扁平的对象数组,而不是嵌套对象

var count = data.reduce(function(prev, cur) {
  if (prev.hasOwnProperty(cur.type))
    prev[cur.type] += 1;
  else
    prev[cur.type] = 1;

  return prev;
}, {});

您可以使用
数组。reduce
将数据数组缩减为对象映射

这是假设它是一个扁平的对象数组,而不是嵌套对象

var count = data.reduce(function(prev, cur) {
  if (prev.hasOwnProperty(cur.type))
    prev[cur.type] += 1;
  else
    prev[cur.type] = 1;

  return prev;
}, {});

您还可以使用
reduce

objs.reduce(function(c, d){
  if (!c[d.type]) c[d.type] = 0;
  c[d.type]++;
  return c;
}, {});

您还可以使用
reduce

objs.reduce(function(c, d){
  if (!c[d.type]) c[d.type] = 0;
  c[d.type]++;
  return c;
}, {});

请确保我理解您的问题,让我们考虑以下低效的直接实现:

var data = [{address:"9001 Lake...", ..., type:"Medic Response"}, 
            {address:"...", ..., type:"Aid Response",
            ...,
            {address""...", ..., type:"Medic Response"}];
var i, j, b, result = [] ;
for(i = 0; i < data.length; i ++)
{
    b = false ;
    for(j = 0; j < result.length; j++)
       if(result[j].key == data.type) {
           result[j].count += 1 ;
           b = true ;
           break ;
       }
     if (!b) {
        result.push({key:data.type, count: 1});
     }
}
// result is the frequency array of the "type" values in data array
var data=[{地址:“9001湖…”,…,键入:“医疗响应”},
{地址:“…”,…,键入:“援助响应”,
...,
{地址“…”,…,类型:“Medic Response”}];
var i,j,b,结果=[];
对于(i=0;i<>代码> < P>只是为了确保我理解你的问题,让我们考虑以下低效的直接实现:

var data = [{address:"9001 Lake...", ..., type:"Medic Response"}, 
            {address:"...", ..., type:"Aid Response",
            ...,
            {address""...", ..., type:"Medic Response"}];
var i, j, b, result = [] ;
for(i = 0; i < data.length; i ++)
{
    b = false ;
    for(j = 0; j < result.length; j++)
       if(result[j].key == data.type) {
           result[j].count += 1 ;
           b = true ;
           break ;
       }
     if (!b) {
        result.push({key:data.type, count: 1});
     }
}
// result is the frequency array of the "type" values in data array
var data=[{地址:“9001湖…”,…,键入:“医疗响应”},
{地址:“…”,…,键入:“援助响应”,
...,
{地址“…”,…,类型:“Medic Response”}];
var i,j,b,结果=[];
对于(i=0;i
您使用的是jQuery吗?它是对象的平面数组还是递归数组?@ste2425我相信它是平面的,谢谢
str.match(/“type”:“[\w\s]+”,/)
对于非格式化的JSON,根据需要调整空白。您使用的是jQuery吗?它是对象的平面数组还是递归数组?@ste2425我相信它是平面的,谢谢
str.match(/“type”:“[\w\s]+”,/)
对于非格式化JSON,根据需要调整空白。太棒了!这非常有效。我有同样的想法将类型存储在一个单独的对象中:作为键值对计数,但不熟悉forEach或hasOwnProperty函数。非常感谢。是的,这很有希望,但您需要考虑到并非所有字符串值都是例如“原型”、“原型”、“构造函数”。如果您确定没有踩到它们,那么解决方案是完美的。太棒了!这非常有效。我有同样的想法来存储类型:在单独的对象中作为键值对计数,但不熟悉forEach或hasOwnProperty函数。非常感谢。是的,这非常有希望,但您需要考虑到并非所有字符串值都是允许使用“原型”、“原型”、“构造器”。如果您确定没有踩到它们,那么解决方案是完美的。