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
Sonu06Sonu06 

.push

what is the use of .push in lighnting controller??
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sonal,

Greetings to you!

It is a JavaScript Array push method. push() function is used to push one or more values into the array. This function changes the length of the array by the number of elements added to the array. The syntax of the function is as follows:
arr.push(element1[, ...[, elementN]])

The push() method adds one or more elements to the end of an array and returns the new length of the array.
 
const animals = ['pigs', 'goats', 'sheep'];

const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

https://www.w3schools.com/jsref/jsref_push.asp

https://appdividend.com/2018/10/08/javascript-array-push-example-array-prototype-tutorial/

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
Ajay K DubediAjay K Dubedi
Hi Sonal,

* Please refer through below link, it crearly explain the use of .Push operation in Lightning Controller: 

    https://salesforce.stackexchange.com/questions/184465/how-to-add-an-item-to-an-array-to-show-in-lightning-component

    https://stackoverflow.com/questions/51443599/how-to-add-more-data-an-array-of-objects-in-a-lightning-component
    
eg.,  
  controller.js---->
    
    ({
    doInit: function(component, event, helper) {
    helper.populateObjectList(component, event);
    },

    handleClick: function(component, event, helper) {
    helper.augmentObject(component, event);
    }
   })

   helper.js---->
   
  ({
  populateObjectList: function(component, event) {
    var objs = [];
    objs.push(this.createObj('iPhone', 34000));
    objs.push(this.createObj('Motox', 27000));
    objs.push(this.createObj('Oneplus', 33033));
    component.set('v.objs', objs);
  },

  createObj: function(product, price) {
    var obj = new Object();
    obj.product = product;
    obj.price = price;
    return obj;
  },

  augmentObject: function(component, event) {
    var objs = component.get('v.objs');
    objs.push(this.createObj('Demo', 1000));
    component.set('v.objs', objs);
  }
  })
  
  Component---->
  
  <!-- lightning component -->
  <!-- component A -->
  <aura:component>
  <aura:attribute name="objs" type="Object[]" />
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  <table>
    <tr>
      <th>Product</th>
      <th>Price</th>
    </tr>
    <aura:iteration items="{!v.objs}" var="obj">
      <tr>
        <td>{!obj.product}</td>
        <td>{!obj.price}</td>
      </tr>
    </aura:iteration>
  </table>
  <!-- component B -->
  <lightning:button variant="brand" label="Add another" title="Brand action" onclick="{! c.handleClick }" />
  </aura:component> 
     
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha
Hi Sonal,

I have through your question. Push is the method of javascript used to add the element in the array. Push is the same as the add method in the java. 
Some points about push methods are:-
1:- The push() method adds new items to the end of an array and returns the new length. 
2:- The new item(s) will be added at the end of the array.
3:- You can also more the one item at a time.

Example:--

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi", "Lemon", "Pineapple");

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com