I am using AngularJS to render html page doing ajax call, which returns json file.
How to get count of id's in below JSON using angularjs?
[{"id":1,"Name":"Apple"},
{"id":2,"Name":"Mango"},
{"id":3,"Name":"Banana"}
{"id":4,"Name":"Coconut"}
{"id":5,"Name":"pineaple"}
{"id":6,"Name":"Orange"}
{"id":7,"Name":"Guava"}]
To obtain the size of a JSON-encoded array or object, use the json_size function, and specify the column containing the JSON string and the JSONPath expression to the array or object.
Use measureLength() to compute the number of characters that will be printed by printTo() . Use measurePrettyLength() to compute the number of characters that will be printed by prettyPrintTo() .
JsonArray::size() gets the number of elements in the array pointed by the JsonArray . If the JsonArray is null, this function returns 0 . Internally, this function walks a linked-list to count the elements, so its time complexity is O(n). Don't use this function to create a for loop; instead, use iterators.
Use len() to count the items in a JSON object Call len(obj) to return the number of items in a JSON object obj .
You don't need Angular to get the length of that.
In this case, your parsed JSON is a simple array, you can get the length with:
obj.length
assuming that obj
contains your parsed JSON as such:
var obj = [{"id":1,"Name":"Apple"},
{"id":2,"Name":"Mango"},
{"id":3,"Name":"Banana"}
{"id":4,"Name":"Coconut"}
{"id":5,"Name":"pineaple"}
{"id":6,"Name":"Orange"}
{"id":7,"Name":"Guava"}]
var json=
[{"id":1,"Name":"Apple"},
{"id":2,"Name":"Samsung"},
{"id":3,"Name":"Nokia"}
{"id":4,"Name":"Motorola"}]
var count = Object.keys(json).length;
console.log(count);
In your HTML, lets say your array is "comments" (a common use-case):
{{ comments.length }}
You can also do a ng-hide="{{ comments.length}}"
on an element that says something to the effect of "no comments".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With