Thursday, September 4, 2025
HomeLanguagesJavascriptHow to remove object from array of objects using JavaScript ?

How to remove object from array of objects using JavaScript ?

Given a JavaScript array containing objects in and the task is to delete certain objects from an array of objects. There are two approaches to solving this problem which are discussed below: 

Approach 1:

  • Use array.forEach() method to traverse every object of the array.
  • For each object, use delete obj.property to delete the certain object element from an array of objects.

Example: This example implements the above approach. 

Javascript




let arr = [{
    a: 'Val_1',
    b: 'Val_2'
}, {
    a: 'Val_3',
    b: 'Val_4'
}, {
    a: 'Val_1',
    b: 'Val_2'
}];
 
function myFunc() {
    arr.forEach(function (obj) {
        delete obj.a
    });
 
    console.log(JSON.stringify(arr));
}
 
myFunc();


Output

[{"b":"Val_2"},{"b":"Val_4"},{"b":"Val_2"}]

Approach 2:

  • Use array.map() method to traverse every object of the array.
  • For each object use delete obj.property to delete the certain object from array of objects.

Example: This example implements the above approach. 

Javascript




let arr = [{
    a: 'Val_1',
    b: 'Val_2'
}, {
    a: 'Val_3',
    b: 'Val_4'
}, {
    a: 'Val_1',
    b: 'Val_2'
}];
 
function myFunc() {
    arr.map(function (obj) {
        delete obj.a;
        return obj;
    });
 
    console.log(JSON.stringify(arr));
}
 
myFunc();


Output

[{"b":"Val_2"},{"b":"Val_4"},{"b":"Val_2"}]
RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6627 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS