Get count of true values in JSON with javascript
Use reduce
to iterate over the values
of each object, extract the active
property from each, and add that boolean to the accumulator, which will coerce it to a number:
const obj = { filters: { module: { value: "All", active: false }, dates: { value: [], active: true }, foo: { value: "All", active: false }, bar: { value: [], active: true } }};
console.log( Object.values(obj.filters).reduce((a, { active }) => a + active, 0));
How to count values in JSON object?
You can do it like this if all you want is to count them.
var count = 0;
for (var key in obj.row) {
if (key.dimension) {
count++;
}
}
console.log(count);
Javascript: Get count of values in JSON
First of all, convert your JSON array to an array object:
var json_ar = '[100, 100, 101, 100, 101, 101, 101, 101]';
var ar = JSON.parse(json_ar);
From then on, it's simply counting, which depends a little bit on what the objects in your array actually are. If they're numbers, a simplistic
var counts = {};
ar.forEach(function(v) {
counts[v] = counts[v] ? counts[v] + 1 : 1;
});
will get you counts = {'100': 3, '101': 5}
.
Count number of elements with certain properties inside JSON
You can use the filter
function to filter an array of objects :
var data = {...}
data.humans.filter(function(o) { return o.hairs == 2 }).length
//Return the number of humans who have 2 hairs
Take a look fiddle
Count number of values in json Angular 5
You can use filter() to get the array and then use length:
The filter() method creates a new array with all elements that pass
the test implemented by the provided function.
var obj = { "tasks": [ { "name": "Moj Zadatak", "finished": true, "id": 1 }, { "name": "Moj Zadatak", "finished": true, "id": 2 }, { "name": "Novo", "finished": true, "id": 3 }, { "name": "Prepravka", "finished": true, "id": 4 }, { "name": "Prepravka", "finished": true, "id": 5 }, { "name": "Najnovije", "finished": false, "id": 6 }, { "name": "Hajde", "finished": false, "id": 7 }, { "name": "Hajde", "finished": false, "id": 8 }, { "name": "Novi Task", "finished": false, "id": 9 } ]}
let finishedValues = obj.tasks.filter(n => n.finished).length;let unfinishedValues = obj.tasks.filter(n => !n.finished).length;
console.log(finishedValues);console.log(unfinishedValues);
Count the number of items in json with conditions
Well, you can do this without any 3rd party library and also without looping:
array.filter(function(value) { return value.age === 23 }).length;
And with ES6 it even becomes more terse
array.filter(value => value.age === 23).length;
Counting instances of key value pairs in JSON file in JavaScript
You can use a key/value hash of type names/counts, and then iterate over your collection of objects, incrementing each count as you go. For example:
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);
Related Topics
Generate a Weighted Random Number
How to Display JavaScript Datetime in 12 Hour Am/Pm Format
How to Wrap Each Word of an Element in a Span Tag
How to Refresh an Angular Component
How to Append Whole Set of Model to Formdata and Obtain It in MVC
Get Weeks in Month Through JavaScript
How to Intercept Button Click Inside Uiwebview on Ios
Hide Options in Select2 Multi Select
How to Use Lodash to Remove Keys from a Json Object
Angular 4 - Cannot Read Property Status of Null While Displaying Validation Error
How to Push Value Pair into Object Using Method
Angular:How to Add/Remove Files in the Angular
How to Pass Checkbox State to Onclick Function in React
Reload Datatable After Ajax Success