function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tina sharma 10tina sharma 10 

find length of attribute in lightning

I have a json array as follows in lightning component controller.

  var providerObj = [];
      providerObj=[{
                            "specialties": [
                                 {
                                      "type": "specialty",
                                      "name": "Cardiology",
                                      "id": "839"
                                 },
                                    {                            
                                      "type": "specialty",
                                        "name": "Pediatric Cardiology",
                                     "id": "824"
                                    },
                                  
                                 {
                                      "type": "specialty",
                                       "name": "Pediatric Cardiothoracic Surgery",
                                      "id": "707"
                                  }
                          ],
                               "procedures": [
                                                {
                                                  "type": "procedure",
                                                 "name": "Established Patient Office Visit (Interventional Cardiology)",
                                                 "id": "1735"
                                                },
                                             {
                                                 "type": "procedure",
                                                 "name": "Established Patient Office Visit (Pediatric Cardiology)",
                                                  "id": "1768"
                                                },
                                             
                                             {
                                                  "type": "procedure",
                                                   "name": "Office Consultation ONLY for new or established patients, no testing (Cardiology)",
                                                   "id": "1499"
                                                }
                                            ],
                                          "providers": [
                                    {
      "type": "facility",
      "name": "Eastern CT Cardiology Diagnostic",
      "id": "0007302322",
      "default_procedure": null
    },
    {
      "type": "facility",
      "name": "NY Comprehensive Cardiology, LLC",
      "id": "0007816766",
      "default_procedure": null
    },
    
   
  ]
        }];
     
        component.set("v.allResults",providerObj);
Here allResults attribute is of the following type
<aura:atttrbute name="allResults" type="Object"/>

I want to get the length of specialities , procedures and providers and also the total number of JSON records.

I am doing like this in controller

var arrayCount = providerObj.length;
var specCount = providerObj.specialities.length;

But whatever I do I am not getting the count of records.

Please help me regarding this

tina
        
Best Answer chosen by tina sharma 10
Deekshant SharmaDeekshant Sharma
Hi Tina,
 
providerObj.length

This above statement should give you the expected length of the providerObj array which is 1 as it has a single element which is an object.
However, to get the length of the property 'specialities' , you must write it like:
providerObj[0].specialties.length

providerObj[0] will return the first element of the array which is an object and after that you should be able to access the specialities property.

Thanks,
Deekshant

All Answers

Deekshant SharmaDeekshant Sharma
Hi Tina,
 
providerObj.length

This above statement should give you the expected length of the providerObj array which is 1 as it has a single element which is an object.
However, to get the length of the property 'specialities' , you must write it like:
providerObj[0].specialties.length

providerObj[0] will return the first element of the array which is an object and after that you should be able to access the specialities property.

Thanks,
Deekshant

This was selected as the best answer
tina sharma 10tina sharma 10
Hi Deekshant

Thanks, This worked.