7 ways to Remove object from array in Javascript

In this post, we are going to learn 7 ways to Remove object from array in Javascript with examples.We are going to use array.pop(), array.shift(), array.filter(), array.slice(), array.splice() and map() and foreach loop.

1. Remove First object from an array in Javascript


In this JavaScript example, We are removing the first object from an array of objects by calling the array.shift() method.Let us understand with the below example.

let Employee =[
    {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
    {Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
    {Empname: 'Racx', salary: 60000,EmpID:3,Dep:'IT'},
    {Empname: 'Max', salary: 70000,EmpID:4,Dep:'IT'}  ]


Employee.shift(); 
console.log(Employee)

Output

[
  { Empname: 'jack', salary: 50000, EmpID: 2, Dep: 'IT' },
  { Empname: 'Racx', salary: 60000, EmpID: 3, Dep: 'IT' },
  { Empname: 'Max', salary: 70000, EmpID: 4, Dep: 'IT' }
]

2. Remove the Last element from array of object


In this JavaScript program, We are removing the last object from an array object by using an array.pop() method.The array.pop() return the deleted object from the array. So we are displaying the updated array of objects (Employee)

let Employee =[
    {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
    {Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
    {Empname: 'Racx', salary: 60000,EmpID:3,Dep:'IT'},
    {Empname: 'Max', salary: 70000,EmpID:4,Dep:'IT'}  ]
    

Employee.pop();

console.log(Employee)

Output

[
  { Empname: 'John', salary: 60000, EmpID: 1, Dep: 'Admin' },
  { Empname: 'jack', salary: 50000, EmpID: 2, Dep: 'IT' },
  { Empname: 'Racx', salary: 60000, EmpID: 3, Dep: 'IT' }
]

3. Remove object from array in Javascript


In this example, We are removing from an array of objects by using an array.splice() method.First, we have found the index of values from the object and here we have passed the splice method to the index that we want to delete and deletecount the number of elements starting from the index.

Another way of deleting an object from an array of objects using splice with map() function and indexof() and finally passing the index to splice() method to detele element on this index.

let Employee =[
    {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
    {Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
    {Empname: 'Racx', salary: 60000,EmpID:3,Dep:'IT'},
    {Empname: 'Max', salary: 70000,EmpID:4,Dep:'IT'}  ]
 


const Index = Employee.findIndex( item => item.EmpID === 4 );

if (index > -1) 
{
Employee.splice( Index, 1 );
}

console.log(Employee)

//using map() function 
var Index = Employee.map(item=> 
{ return item.EmpId; }).indexOf(4);


Employee.splice(Index, 1);

console.log(Employee)

Output

[
  { Empname: 'John', salary: 60000, EmpID: 1, Dep: 'Admin' },
  { Empname: 'jack', salary: 50000, EmpID: 2, Dep: 'IT' },
  { Empname: 'Racx', salary: 60000, EmpID: 3, Dep: 'IT' }
]

4. Slice() to Remove object from array in Javascript


In this Javascript Program, we have used an array.slice() method to remove an object from an array.The array.slice().It returns a shallow copy of the array into a new array from start to end index.

let Employee =[
    {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
    {Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
    {Empname: 'Racx', salary: 60000,EmpID:3,Dep:'IT'},
    {Empname: 'Max', salary: 70000,EmpID:4,Dep:'IT'}  ]
 




var newArray = Employee.slice(2, 4);
console.log(newArray)

Output

[
  { Empname: 'Racx', salary: 60000, EmpID: 3, Dep: 'IT' },
  { Empname: 'Max', salary: 70000, EmpID: 4, Dep: 'IT' }
]

5. Array.filter() to remove object from array in Javascript


In this Javascript program, we have used array.filter() method that returns an object from the array where empId not equals 1.

let Employee =[
    {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
    {Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
    {Empname: 'Racx', salary: 60000,EmpID:3,Dep:'IT'},
    {Empname: 'Max', salary: 70000,EmpID:4,Dep:'IT'}  ]
 

var updateobj = Employee.filter((elem) => elem.EmpID!== 1);
console.log(updateobj);

Output

[
  { Empname: 'jack', salary: 50000, EmpID: 2, Dep: 'IT' },
  { Empname: 'Racx', salary: 60000, EmpID: 3, Dep: 'IT' },
  { Empname: 'Max', salary: 70000, EmpID: 4, Dep: 'IT' }
]

6. Foreach loop to Remove object from array in Javascript


In this example we have used foreach loop to iterate over an array of objects, we are checking the conditions where empID is not equal to 4. It will push all employees except the employee EmpID==4.

let Employee =[
    {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
    {Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
    {Empname: 'Racx', salary: 60000,EmpID:3,Dep:'IT'},
    {Empname: 'Max', salary: 70000,EmpID:4,Dep:'IT'}  ]
 

var resultArr = [];
Employee.forEach(function(data){
    if(data.EmpID!== 4){
        resultArr.push(data);
    }
});
console.log(resultArr);

Output

[
  { Empname: 'John', salary: 60000, EmpID: 1, Dep: 'Admin' },
  { Empname: 'jack', salary: 50000, EmpID: 2, Dep: 'IT' },
  { Empname: 'Racx', salary: 60000, EmpID: 3, Dep: 'IT' }
]

7. Remove object From another array of objects


To remove objects based on another array of objects.We have used array.filter().Let us understand with the below example.

let Employee =[
    {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
    {Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
    {Empname: 'Racx', salary: 60000,EmpID:3,Dep:'IT'},
    {Empname: 'Max', salary: 70000,EmpID:4,Dep:'IT'}  ]
    
Employee2 = [{Empname: 'Max', salary: 70000,EmpID:4,Dep:'IT'} ]


 
 
var resultArr = Employee.filter(x => !Employee2.filter(y => y.EmpID === x.EmpID).length); 

console.log(resultArr);

Output

[
  { Empname: 'John', salary: 60000, EmpID: 1, Dep: 'Admin' },
  { Empname: 'jack', salary: 50000, EmpID: 2, Dep: 'IT' },
  { Empname: 'Racx', salary: 60000, EmpID: 3, Dep: 'IT' }
]