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
Fred13Fred13 

How do you get the size of a list/array in lightning controller?

Hi.  I actually have two quesitons.  First, is the following a list or array?

var gs = []

Second, how do I determine the size of that variable?  console.log(gs.size()); renders an error

Finally, how do I determine the size of an attribute for a custom oject.  For example, I have the following attribute on my component:

<aura:attribute name="newGroupStructures" type="Group_Structure__c[]"/>

How can I write a console.log to see the size of that?

thanks!!!!

Fred
Best Answer chosen by Fred13
Khan AnasKhan Anas (Salesforce Developers) 
Hi Fred,

Greetings to you!

Size of the array:
var newItems=[];
console.log('Size of newItems = ' + newItems.length);

//load array with new data
newItems.push(response.getReturnValue());
console.log('Size of  newItems = ' + newItems.length);

Size of an attribute:
var ngs = component.get("v.newGroupStructures");
console.log("Size of newGroupStructures = " + ngs.length);

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Fred,

Greetings to you!

Size of the array:
var newItems=[];
console.log('Size of newItems = ' + newItems.length);

//load array with new data
newItems.push(response.getReturnValue());
console.log('Size of  newItems = ' + newItems.length);

Size of an attribute:
var ngs = component.get("v.newGroupStructures");
console.log("Size of newGroupStructures = " + ngs.length);

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Fred13Fred13
Thank you so much for the quick response.  Oddly, I am getting "undefined" for one of the variables and I'm not sure why.  Both the gs length and the CloneNumber length provide a value.  However, teh CloneGS length is coming up as undefined.  Any idea why this would be?

Here are how the attributes are defined on the component:
<aura:attribute name="existinggroupstructure" type="Group_Structure__c[]" />
    <aura:attribute name="CloneNumber" type="Integer"/>
Fred13Fred13
Sorry, I posted but was not finished.  Here is the doInit in the controller:
 
doInit : function(component, event, helper) {
	var CloneGS = component.get("v.existinggroupstructure");
    var CloneNumber = component.get("v.CloneNumber");
    var gs = [];
     for (var i = 0; i < CloneNumber; i++) {
         gs.push(CloneGS);
     }

      //the following component.set updates the newGroupStructures attribute with the number of records selected
      component.set("v.newGroupStructures", gs); 
      
      console.log('### gs length ' + gs.length);
      console.log('### ClongGS length ' + CloneGS.length);
      console.log('### CloneNumber ' + CloneNumber.length);
     // console.log("### here is the newGroupStructures " + JSON.stringify(component.get("v.newGroupStructures")));
     // console.log('### here is the existinggroupstructure ' + JSON.stringify(component.get("v.existinggroupstructure")));
      
        //reset the cloneNumber
        CloneNumber = 0; 
  },

 
Raj VakatiRaj Vakati
Try this
 
doInit : function(component, event, helper) {
	var CloneGS = component.get("v.existinggroupstructure");
    var CloneNumber = component.get("v.CloneNumber");
    var gs = [];
     for (var i = 0; i < CloneNumber; i++) {
         gs.push(CloneGS);
     }

      //the following component.set updates the newGroupStructures attribute with the number of records selected
      component.set("v.newGroupStructures", gs); 
      
      console.log('### gs length ' + gs.length);
      console.log('### ClongGS length ' + CloneGS.length);
      console.log('### CloneNumber ' + CloneNumber);
     // console.log("### here is the newGroupStructures " + JSON.stringify(component.get("v.newGroupStructures")));
     // console.log('### here is the existinggroupstructure ' + JSON.stringify(component.get("v.existinggroupstructure")));
      
        //reset the cloneNumber
        CloneNumber = 0; 
  },

 
Fred13Fred13
Its actually the CloneGS that is coming up undefined.