How to use Array.filter() method In Javascript
The JavaScript Array filter() Method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method.
Syntax:
array.filter(callback(element, index, arr), thisValue)
Example:
function deleteObjects() {
// Declaring an associative
// array of objects
let arr = new Object();
// Adding objects in array
arr['key'] = 'Value';
arr['geeks'] = 'w3wiki';
arr['name'] = 'JavaScript';
// Checking object exist or not
console.log(arr['name']);
// Removing object from
// associative array
const updatedArray = Object.fromEntries(
Object.entries(arr).filter(([key]) => key !== 'name')
);
// It gives result as undefined
// as object is deleted
return updatedArray;
}
// Calling function
console.log(deleteObjects());
Output
JavaScript { key: 'Value', geeks: 'w3wiki' }
How to remove Objects from Associative Array in JavaScript ?
In this article, we are going to learn about removing Objects from Associative Array in Javascript, In JavaScript, you can remove objects from an associative array (also known as an object) using the following methods.
Table of Content
- Approach 1: Using JavaScript delete operator
- Using JavaScript Array.filter() method
- Using Lodash _.omit method
- Using Object.assign() and Spread Operator