[SalesForce] Remove some elements from an array based on another array js lightning

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[]" />

Best Answer

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

Related Topic