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
Using JavaScript delete operator
Declare an associative array containing key-value pair objects. Then use the delete keyword to delete the array objects from an associative array.
Example 1: This example uses the delete keyword to remove the objects from the associative array.
function deleteObjects() {
// Declaring an associative
// array of objects
let arr = new Object();
// Adding objects in array
arr['key'] = 'Value';
arr['geeks'] = 'GeeksforGeeks';
arr['name'] = 'Rajnish';
// Checking object exist or not
console.log(arr['name']);
// Removing object from
// associative array
delete arr['name'];
// It gives result as undefined
// as object is deleted
console.log(arr['name']);
}
// Calling function
deleteObjects();
Output
Rajnish undefined
Example 2: This example uses the delete keyword to remove the objects from the associative array.
function deleteObjects() {
// Declaring an associative
// array of objects
let arr = new Object();
// Adding objects in array
arr['key'] = 'Value';
arr['geeks'] = 'GeeksforGeeks';
arr['name'] = 'Rajnish';
// Checking object exist or not
console.log(arr['geeks']);
// Removing object from
// associative array
delete arr.geeks;
// It gives result as undefined
// as object is deleted
console.log(arr['geeks']);
}
// Calling function
deleteObjects();
Output
GeeksforGeeks undefined
Using JavaScript Array.filter() method
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'] = 'GeeksforGeeks';
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: 'GeeksforGeeks' }
Using Lodash _.omit method
Lodash is a JavaScript library that works on top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.omit() method is used to return a copy of the object that is composed of the own and inherited enumerable property paths of the given object that are not omitted. It is the opposite of the _.pick() method.
Syntax:
_.omit( object, paths )
Example:
function deleteObjects() {
const _ = require("lodash");
// Declaring an associative
// array of objects
let arr = new Object();
// Adding objects in array
arr['key'] = 'Value';
arr['geeks'] = 'GeeksforGeeks';
arr['name'] = 'JavaScript';
// Checking object exist or not
console.log(arr['key']);
// Removing object from
// associative array
const updatedArray = _.omit(arr, 'key');
// It gives result as undefined
// as object is deleted
return updatedArray;
}
// Calling function
console.log(deleteObjects());
Output:
Value
{ geeks: 'GeeksforGeeks', name: 'JavaScript' }
Using Object.assign() and Spread Operator
To remove an object from an associative array in JavaScript using Object.assign() and the spread operator, destructure the object excluding the desired key. Then, use Object.assign() to merge the destructured object into a new one.
Example: In this example the function demonstrates removing an object from an associative array. It first checks if the object exists, then uses object destructuring to remove the specified key. Finally, it logs the removed object, which returns undefined.
function deleteObjects() {
// Declaring an associative
// array of objects
let arr = new Object();
// Adding objects in array
arr['key'] = 'Value';
arr['geeks'] = 'GeeksforGeeks';
arr['name'] = 'Rajnish';
// Checking object exist or not
console.log(arr['name']);
// Removing object from
// associative array
const removedKey = 'name';
let { [removedKey]: omitted, ...rest } = arr;
arr = rest;
// It gives result as undefined
// as object is deleted
console.log(arr['name']);
}
// Calling function
deleteObjects();
Output
Rajnish undefined
Using Object.keys() and reduce() Method
In this approach, we use the Object.keys() method to get an array of the object's keys, then use the reduce() method to build a new object excluding the key that we want to remove. This method is effective for creating a new object without mutating the original.
Syntax:
Object.keys(obj).reduce((acc, key) => { ... }, {});
Example:
function removeKey(obj, keyToRemove) {
return Object.keys(obj).reduce((acc, key) => {
if (key !== keyToRemove) {
acc[key] = obj[key];
}
return acc;
}, {});
}
const associativeArray = {
id: 1,
name: 'John',
age: 30
};
const keyToRemove = 'age';
const newObject = removeKey(associativeArray, keyToRemove);
console.log(newObject);
// Output: { id: 1, name: 'John' }
Output
{ id: 1, name: 'John' }