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.

2
  • 2
    where are you passing the "Warning" value? Your code and testdata is completely different Commented May 20, 2019 at 17:07
  • 1
    Your array is not multidimensional. Commented May 20, 2019 at 18:27

6 Answers 6

10

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

Sign up to request clarification or add additional context in comments.

8 Comments

I like how you grouped the data by issue type +1.
I like you're grouping too - how would you do this with a nested object? for example, if I wanted the more_data.this_type key/value? var testData = [{ issue_type: "Warning", created_date: "2019-05-13T13:43:16.437Z" ,more_data:{this_type:"value"}}, { issue_type: "Warning", created_date: "2019-05-13T13:45:16.330Z" } ,more_data:{this_type:"value"}}, { issue_type: "Alert", created_date: "2019-05-13T13:43:16.437Z" }, { issue_type: "Alert", created_date: "2019-05-13T13:45:16.330Z" ,more_data:{this_type:"value"}}]
@SonicMotion, take a nested destructuring: { more_data: { this_type: key } }.
in this case, you need a default object, like { more_data: { 'this-type': key } = {} }.
@SonicMotion, your problem is not to have hyphens inside, but not to have an object for getting a property. by having hyphens, you need to rename the property into a valid variable, like here with key.
|
8

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>

Comments

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.

Comments

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)

Comments

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)

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.