4

I have referred these questions listed below, but didn't helped me out.

  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

I want to get count from object with specific value. For example, I have this array list,

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",
  }
]

I want to count how many objects with key "issue_type"="Warning" are there.

I tried with this loop but it is not what I'm looking for,

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']);

Please suggest me the way out.

Keyur
  • 1,113
  • 1
  • 23
  • 42

6 Answers6

8

You could take the value as key for counting.

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
7

You can simply use filter.

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>
Moritz Roessler
  • 8,542
  • 26
  • 51
2

You could do helper functions which can do that for you based on Array.reduce or Array.filter. For example:

const 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 would count the different values of the props based on the passed key. countValue would only count specific value by simply filtering the passed array.

Mo.
  • 26,306
  • 36
  • 159
  • 225
Akrion
  • 18,117
  • 1
  • 34
  • 54
1

use .filter() and get array length from the array returned by filter.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

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)
Junius L
  • 15,881
  • 6
  • 52
  • 96
1

In this instance Array.reduce() is your best friend. You should try something like :

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

In the vein of the code snippet of your question, the following serve as a start. The basoc idea:

  • Iterate over the data array
  • Check for the presence of the property of interest
  • Check whether the property contains the value of interest.

Exactly those items that match the criteria are counted.

Your sample code snippet suggests that you might want to refresh your knowledge of JS syntax. You might also want to have a look at the built-in methods operating on Arrays (eg. on MDN) which are used in the other answers

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' ));
collapsar
  • 17,010
  • 4
  • 35
  • 61