1

Hi i have two arrays of type account list and I want to remove accRecords from accountList in js controller lightning. I suppose we need to use splice function based on some index, but don't know how to achieve this.

<aura:attribute name="accRecords" type="Account[]" />
<aura:attribute name="accountList" type="Account[]" />

2 Answers 2

1

You can use Javascript Filter to remove same records.

 var accrecords = component.get('v.accRecords');
 var accountList= component.get('v.accountList');
 var accrecordsId;
 for(i = 0; i < accrecords.length ; i++){
       accrecordsId.push(accrecords[i].id);
 }
 accountList = accountList.filter( function( acc) {
  return ! accrecordsId.includes( acc.id );
} );
component.set('v.accountList',accountList );

reference :- https://stackoverflow.com/questions/19957348/javascript-arrays-remove-all-elements-contained-in-another-array

2
  • i did not get what acc is here?
    – Salesforce
    Commented Aug 12, 2017 at 8:14
  • acc is an element of array accountList which is being compared Commented Aug 12, 2017 at 8:23
0
    var myArray = component.get("v.accRecords");
            var toRemove = component.get("v.accountList");

            for (var i = myArray.length - 1; i >= 0; i--) {
                for (var j = 0; j < toRemove.length; j++) {
                    if (myArray[i] && (myArray[i].Id === toRemove[j].Id)) {
                        myArray.splice(i, 1);
                    }
                }
            }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .