Remove specific properties from Array objects | JavaScript

Managing data often involves the need to remove specific elements from JavaScript arrays, and while there is no single built-in 'remove' method, there are various techniques available to achieve this task effectively. Removing properties or fields from an array of objects is particularly crucial when dealing with sensitive tabular data.

One common approach to remove items from an array of objects is to use the filter() method along with a callback function that specifies the condition for removal. The filter() method creates a new array with elements that pass the given condition, effectively omitting the unwanted items from the original array.


How to remove property from an array of objects in JavaScript?

Using array.forEach()

var array = [{"Product": "Product-1", "Description":"Description-1"}, {"Product": "Product-2", "Description":"Description-2"}, {"Product": "Product-3", "Description":"Description-3"}]; array.forEach(function(tmp){ delete tmp.Description }); console.log(array);
Output:
[ { Product: 'Product-1' }, { Product: 'Product-2' }, { Product: 'Product-3' } ]

Method - 2

var array = [{"Product": "Product-1", "Description":"Description-1"}, {"Product": "Product-2", "Description":"Description-2"}, {"Product": "Product-3", "Description":"Description-3"}]; array.forEach(u => delete u.Description); console.log(array);
Output:
[ { Product: 'Product-1' }, { Product: 'Product-2' }, { Product: 'Product-3' } ]

Using Array.prototype.map()

The map() method is used to create a new array with the results of calling a provided function on each element of the original array. It iterates through each element in the array, invokes the specified callback function on each element, and constructs a new array containing the results returned by the callback function.

var array = [{"Product": "Product-1", "Description":"Description-1"}, {"Product": "Product-2", "Description":"Description-2"}, {"Product": "Product-3", "Description":"Description-3"}]; array.map(function(item) { delete item.Description; return item; }); console.log(array);
Output:
[ { Product: 'Product-1' }, { Product: 'Product-2' }, { Product: 'Product-3' } ]

Conclusion

Using the map() method enables concise and functional programming approaches, making it a valuable tool for various array manipulation tasks in JavaScript.