How to Count Json Object Item

Get total number of items on Json object?

In addition to kieran's answer, apparently, modern browsers have an Object.keys function. In this case, you could do this:

Object.keys(jsonArray).length;

More details in this answer on How to list the properties of a javascript object

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

How to count the number of objects in .json

Put your JSON data in JSON.parse and then you can access length property on it:

JSON.parse(JSON.stringify(data)).length

How to count json object in javascript

You can try this:

Object.keys(jsonArray).length;

to get the number of items in your JSON object.

Also refer Object.keys

Object.keys() returns an array whose elements are strings
corresponding to the enumerable properties found directly upon object.
The ordering of the properties is the same as that given by looping
over the properties of the object manually.

How to count JSON objects

That's an array.

You can parse it (JSON.parse), then use the length property.

how to count similar value object in json?

How about:

let a = [
{ "name": "ankit", "DOB": "23/06" },
{ "name": "kapil", "DOB": "26/06" },
{ "name": "ankit", "DOB": "27/06" }
];

let count = 0;
a.forEach(item => {
if (item.name === "ankit") {
count++;
}
});

(code in playground)

How to count the number of elements present in a JSON object?

You just need to make sure there is no empty values inside that array. Because when empty string is exploded it will create one element with value as blank.

Remove those values using array_filter

echo count(array_filter(explode(",", $data->posts_id_fr)));

// Will print 0

Just in case if you want to loop through as suggested by GrumpyCrouton

foreach($data as $key => $value)
{
echo $key . ' : ' . count(array_filter(explode(",", $value))) . PHP_EOL;
}

Output:
posts_id_en : 3
posts_id_fr : 0
reports_id_en: 2
reports_id_fr: 1

get size of JSON object

You can use something like this

var myObject = {'name':'Kasun', 'address':'columbo','age': '29'}

var count = Object.keys(myObject).length;
console.log(count);

Javascript group a JSON object by two properties and count

You can use library such as lodash and use it's group by function to do this easy way, bit time consuming way would be to implement your own group by function.

let long_array = [{
"location": "Kirrawee",
"identity_long": "student"
},
{
"location": "Kirrawee",
"identity_long": "visitor"
},
{
"location": "Kirrawee",
"identity_long": "visitor"
},
{
"location": "Kirrawee",
"identity_long": "worker"
},
{
"location": "Sutherland",
"identity_long": "student"
},
{
"location": "Sutherland",
"identity_long": "resident"
},
{
"location": "Sutherland",
"identity_long": "worker"
},
{
"location": "Sutherland",
"identity_long": "resident"
},
{
"location": "Miranda",
"identity_long": "resident"
},
{
"location": "Miranda",
"identity_long": "worker"
},
{
"location": "Miranda",
"identity_long": "student"
},
{
"location": "Miranda",
"identity_long": ""
},
{
"location": "Miranda",
"identity_long": "worker"
},
{
"location": "Miranda",
"identity_long": "resident"
}
];

function addItemCounts(items, groupByKeys) {
var groups = _.groupBy(long_array, obj => {
return groupByKeys.map(key => obj[key]).join('-');
});

return _.map(groups, g => ({
...g[0],
count: g.length
}));
}

console.log(addItemCounts(long_array, ['identity_long', 'location']));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>


Related Topics



Leave a reply



Submit