JavaScript-获取具有特定值的对象计数

时间:2019-05-20 17:05:48

标签: javascript arrays javascript-objects

我已经提到了下面列出的这些问题,但是并没有帮助我。

  1. Counting the occurrences / frequency of array elements
  2. Count values of the inner two-dimensional array - javascript
  3. Count elements in a multidimensional array

我想从具有特定值的对象中获取计数。例如,我有这个数组列表,

var testData = [
  {
    "issue_type": "Warning",
    "created_date": "2019-05-13T13:43:16.437Z",
  },
  {
    "issue_type": "Warning",
    "created_date": "2019-05-13T13:45:16.330Z",
  },
  {
    "issue_type": "Alert",
    "created_date": "2019-05-13T13:43:16.437Z",
  },
  {
    "issue_type": "Alert",
    "created_date": "2019-05-13T13:45:16.330Z",
  }
]

我想计算有多少个带有键"issue_type"="Warning"的对象。

我尝试了这个循环,但这不是我想要的,

var counts = {};
for (var i = 0; i < arr.length; i++) {
    var num = arr[i];
    counts[num] = counts[num] ? counts[num] + 1 : 1;
}
console.log(count['issue_type']);

请给我建议出路。

6 个答案:

答案 0 :(得分:3)

您可以将值用作计数的键。

var testData = [{ issue_type: "Warning", created_date: "2019-05-13T13:43:16.437Z" }, { issue_type: "Warning", created_date: "2019-05-13T13:45:16.330Z" }, { issue_type: "Alert", created_date: "2019-05-13T13:43:16.437Z" }, { issue_type: "Alert", created_date: "2019-05-13T13:45:16.330Z" }],
    counts = testData.reduce((c, { issue_type: key }) => (c[key] = (c[key] || 0) + 1, c), {});

console.log(counts);

答案 1 :(得分:1)

使用.filter()并从过滤器返回的数组中获取数组长度。

  

filter()方法创建一个新数组,其中包含所有通过提供的功能实现的测试的元素。

var testData = [
  {
    "issue_type": "Warning",
    "created_date": "2019-05-13T13:43:16.437Z",
  },
  {
    "issue_type": "Warning",
    "created_date": "2019-05-13T13:45:16.330Z",
  },
  {
    "issue_type": "Alert",
    "created_date": "2019-05-13T13:43:16.437Z",
  },
  {
    "issue_type": "Alert",
    "created_date": "2019-05-13T13:45:16.330Z",
  }
]

console.log(testData.filter(item => item.issue_type === 'Warning').length)

答案 2 :(得分:1)

在这种情况下,Array.reduce()是您最好的朋友。您应该尝试:

const answer = testData.reduce((accumulator, currentVal) => {
  if (currentVal.issue_type === 'Warning') {
    accumulator += 1;
  }
  return accumulator;
}, 0)

答案 3 :(得分:1)

就您的问题的代码片段而言,以下内容为起点。基本思想:

  • 遍历数据数组
  • 检查是否存在感兴趣的财产
  • 检查该属性是否包含利息值。

准确地计算出符合条件的那些项目。

您的示例代码段建议您可能需要重新了解JS语法。您可能还想看看在其他答案中使用的对数组(例如,在MDN上)进行操作的内置方法

var testData = [
  {
    "issue_type": "Warning",
    "created_date": "2019-05-13T13:43:16.437Z",
  },
  {
    "issue_type": "Warning",
    "created_date": "2019-05-13T13:45:16.330Z",
  },
  {
    "issue_type": "Alert",
    "created_date": "2019-05-13T13:43:16.437Z",
  },
  {
    "issue_type": "Alert",
    "created_date": "2019-05-13T13:45:16.330Z",
  }
];


function countPropValue ( pa_data, ps_prop, ps_value ) {
    let n_count = 0;

    for (var i = 0; i < pa_data.length; i++) {
        if (pa_data[i].hasOwnProperty(ps_prop)) {
            if (pa_data[i][ps_prop] === ps_value) {
                n_count++;
            }
        }
    }
    
    return n_count;
} // countPropValue

console.log(countPropValue ( testData, 'issue_type', 'Warning' ));

答案 4 :(得分:1)

您可以简单地使用过滤器。

console.log (
  testData.filter (({issue_type}) => issue_type === 'Warning').length
)
<script>var testData = [
  {
    "issue_type": "Warning",
    "created_date": "2019-05-13T13:43:16.437Z",
  },
  {
    "issue_type": "Warning",
    "created_date": "2019-05-13T13:45:16.330Z",
  },
  {
    "issue_type": "Alert",
    "created_date": "2019-05-13T13:43:16.437Z",
  },
  {
    "issue_type": "Alert",
    "created_date": "2019-05-13T13:45:16.330Z",
  }
]</script>

答案 5 :(得分:1)

您可以执行辅助功能,这些功能可以基于Array.reduceArray.filter为您完成。例如:

var data = [ { "issue_type": "Warning", "created_date": "2019-05-13T13:43:16.437Z", }, { "issue_type": "Warning", "created_date": "2019-05-13T13:45:16.330Z", }, { "issue_type": "Alert", "created_date": "2019-05-13T13:43:16.437Z", }, { "issue_type": "Alert", "created_date": "2019-05-13T13:45:16.330Z", } ]

let countValuesByKey = (arr, key) => arr.reduce((r,c) => {
  r[c[key]] = (r[c[key]] || 0) + 1
  return r
}, {})

let countValue = (arr, key, value) => arr.filter(x => x[key] === value).length

console.log(countValuesByKey(data, 'issue_type'))
console.log(countValue(data, 'issue_type', 'Warning'))
console.log(countValue(data, 'issue_type', 'Alert'))

countValuesByKey将根据传递的key计算道具的不同值。 countValue仅通过过滤传递的数组来计算特定值。