Remove Objects from Associative Array in JavaScript
In JavaScript, you can remove objects from an associative array by filtering out elements that match specific criteria or keys. This helps in dynamically managing array contents
[Approach 1]: 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.
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
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
[Approach 2]: Array.filter()
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.
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' }Syntax:
array.filter(callback(element, index, arr), thisValue)[Approach 3]: Lodash _.omit
Lodash is a JavaScript library for easier handling of arrays, objects, strings, and more. The _.omit() method creates a copy of an object excluding specified properties, opposite to _.pick().
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' }Syntax:
_.omit( object, paths )[Approach 4]: 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.
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
[Approach 5]: Object.keys() and reduce()
Use Object.keys() to get an object’s keys and reduce() to create a new object excluding the desired key, without mutating the original.
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' }
Syntax:
Object.keys(obj).reduce((acc, key) => { ... }, {});