4

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"}]
11
  • 3
    possible duplicate of Length of Javascript Object (ie. Associative Array) Commented Jul 4, 2014 at 13:17
  • This is so basic that you could have found it in 30 seconds straight. It's not even Angular... Commented Jul 4, 2014 at 13:18
  • 1
    @domokun it is a regular array not an object Commented Jul 4, 2014 at 13:19
  • 3
    did you try Array.length? Commented Jul 4, 2014 at 13:21
  • @charlietfl yes..tried that but it showed error Commented Jul 4, 2014 at 13:23

7 Answers 7

12

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"}]
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

this is the right answer. I had errors with json.length that property 'length' does not exist on type '{}'. Thank you @Akash
3

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".

Comments

0

Everything is good if you count objects using certain key in array. What is happening if you change JSON array with some other keys? Solution i came up is for universal JSON array. For example, i am using $resource to get data: $resource(pathToJSON).query() --> it returns ResourceClass, not JSON array

After that you need to do next:

$scope.resourceClass = $resource(pathToJSON).query();

$scope.resourceClass.$promise --> returns promise object

$scope.resourceClass.$promise.then(function(data) {
      console.log(data.length);
});

data is JSON Array.

Comments

0

Check https://jsfiddle.net/arunsoraba/qfurytmn/6/ sample.

function memorySizeOf(obj) {
    var bytes = 0;

    function sizeOf(obj) {
        if(obj !== null && obj !== undefined) {
            switch(typeof obj) {
            case 'number':
                bytes += 8;
                break;
            case 'string':
                bytes += obj.length * 2;
                break;
            case 'boolean':
                bytes += 4;
                break;
            case 'object':
                var objClass = Object.prototype.toString.call(obj).slice(8, -1);
                if(objClass === 'Object' || objClass === 'Array') {
                    for(var key in obj) {
                        if(!obj.hasOwnProperty(key)) continue;
                        sizeOf(obj[key]);
                    }
                } else bytes += obj.toString().length * 2;
                break;
            }
        }
        return bytes;
    };

    function formatByteSize(bytes) {
        if(bytes < 1024) return bytes + " bytes";
        else if(bytes < 1048576) return(bytes / 1024).toFixed(3) + " KiB";
        else if(bytes < 1073741824) return(bytes / 1048576).toFixed(3) + " MiB";
        else return(bytes / 1073741824).toFixed(3) + " GiB";
    };

    return formatByteSize(sizeOf(obj));
};

var test = [{ name :'aa'},{ name :'aa'},{ name :'aa'},{ name :'aa'},{ name :'aa'},{ name :'aa'},{ name :'aa'},{ name :'aa'},{ name :'aa'}];
var size = memorySizeOf(test);
alert(size);

Comments

0
Json Data:

            { "docNme": "HSL_Invoice_SRG Network_2", "isMultiPage": true, "pgLst": [ { "pgH": "792.0", "pgW": "612.0", "pgNo": "1", "prmLst": [ { "isReference": "false", "ocrKey": "officeAddress", "keyNm": "Office Address", "keyLoc": "34,106,68,9", "keyFont": "", "keyClr": "", "valNm": "", "valLoc": "34,117,176,113", "valFont": "", "valClr": "" }, { "isReference": "false", "ocrKey": "godownAddress", "keyNm": "Godown Address", "keyLoc": "216,106,77,9", "keyFont": "", "keyClr": "", "valNm": "", "valLoc": "216,117,176,113", "valFont": "", "valClr": "" }, { "isReference": "false", "ocrKey": "GSTIN", "keyNm": "GSTIN", "keyLoc": "382,106,30,9", "keyFont": "", "keyClr": "", "valNm": "27AAACH1925Q1ZK", "valLoc": "420,106,94,9", "valFont": "", "valClr": "" }, { "isReference": "false", "ocrKey": "invoiceNo", "keyNm": "Invoice No.", "keyLoc": "382,129,55,9", "keyFont": "", "keyClr": "", "valNm": "56789", "valLoc": "444,129,32,9", "valFont": "", "valClr": "" }, { "isReference": "false", "ocrKey": "poNo", "keyNm": "PO No.", "keyLoc": "382,223,36,9", "keyFont": "", "keyClr": "", "valNm": "HSL-IT-0038-2017/2018", "valLoc": "424,223,118,9", "valFont": "", "valClr": "" } ] }, { "pgH":"792.0", "pgW":"612.0", "pgNo":"2", "prmLst":[ { "isReference": "false", "ocrKey": "officeAddress", "keyNm": "Office Address", "keyLoc": "34,106,68,9", "keyFont": "", "keyClr": "", "valNm": "", "valLoc": "34,117,176,113", "valFont": "", "valClr": "" }, { "isReference": "false", "ocrKey": "godownAddress", "keyNm": "Godown Address", "keyLoc": "216,106,77,9", "keyFont": "", "keyClr": "", "valNm": "", "valLoc": "216,117,176,113", "valFont": "", "valClr": "" }, { "isReference": "false", "ocrKey": "GSTIN", "keyNm": "GSTIN", "keyLoc": "382,106,30,9", "keyFont": "", "keyClr": "", "valNm": "27AAACH1925Q1ZK", "valLoc": "420,106,94,9", "valFont": "", "valClr": "" }, { "isReference": "false", "ocrKey": "invoiceNo", "keyNm": "Invoice No.", "keyLoc": "382,129,55,9", "keyFont": "", "keyClr": "", "valNm": "56789", "valLoc": "444,129,32,9", "valFont": "", "valClr": "" }, { "isReference": "false", "ocrKey": "poNo", "keyNm": "PO No.", "keyLoc": "382,223,36,9", "keyFont": "", "keyClr": "", "valNm": "HSL-IT-0038-2017/2018", "valLoc": "424,223,118,9", "valFont": "", "valClr": "" }] } ] }


Fetch Size :

    let pageLstSize=this.data.pgLst;
        console.log(pageLstSize);

Output : 2

Comments

-3

If obj.length does not work please ensure you are caling it within the appropiate controller.

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.