In JavaScript, associative arrays are generally represented using objects with key-value pairs. You can remove a property using the delete operator or create a new object without the specified property using methods such as filter(), _.omit(), destructuring, or reduce().
- Use delete to directly remove a property from an object.
- Use Object.entries() with filter() to create a new object without the selected property.
- Use _.omit(), destructuring, or reduce() when you want a non-mutating approach.
Approach 1: Using the delete Operator
The delete operator removes a property from an object. It modifies the original object and returns true when the property is successfully deleted.
let arr = {
key: "Value",
geeks: "GeeksforGeeks",
name: "Ryan"
};
console.log(arr.name);
// Remove the name property
delete arr.name;
console.log(arr.name);
Output
Ryan undefined
You can also use bracket notation when the property name is stored in a variable:
const keyToRemove = "geeks";
delete arr[keyToRemove];
console.log(arr);
Approach 2: Using filter() with Object.entries()
The filter() method can be combined with Object.entries() and Object.fromEntries() to create a new object without the specified property.
let arr = {
key: "Value",
geeks: "GeeksforGeeks",
name: "JavaScript"
};
const keyToRemove = "name";
const updatedObject = Object.fromEntries(
Object.entries(arr).filter(([key]) => key !== keyToRemove)
);
console.log(updatedObject);
Output
{ key: 'Value', geeks: 'GeeksforGeeks' }
Syntax:
Object.fromEntries(
Object.entries(object).filter(([key]) => condition)
);
Approach 3: Using Lodash _.omit()
The Lodash _.omit() method creates a new object by excluding the specified property.
const _ = require("lodash");
let arr = {
key: "Value",
geeks: "GeeksforGeeks",
name: "JavaScript"
};
const updatedObject = _.omit(arr, "key");
console.log(updatedObject);
Output:
{ geeks: 'GeeksforGeeks', name: 'JavaScript' }Syntax:
_.omit(object, paths)Approach 4: Using Destructuring and Rest Operator
Object destructuring can be used to remove a property by extracting it and collecting the remaining properties into a new object.
let arr = {
key: "Value",
geeks: "GeeksforGeeks",
name: "Ryan"
};
const keyToRemove = "name";
const { [keyToRemove]: omitted, ...rest } = arr;
console.log(rest);
Output
{ key: 'Value', geeks: 'GeeksforGeeks' }
Here, omitted receives the value of the property being removed, while rest contains all remaining properties.
Approach 5: Using Object.keys() and reduce()
The Object.keys() method retrieves the object's keys, while reduce() creates a new object containing only the properties that should be retained.
function removeKey(obj, keyToRemove) {
return Object.keys(obj).reduce((result, key) => {
if (key !== keyToRemove) {
result[key] = obj[key];
}
return result;
}, {});
}
const associativeArray = {
id: 1,
name: "John",
age: 30
};
const newObject = removeKey(associativeArray, "age");
console.log(newObject);
Output
{ id: 1, name: 'John' }
Syntax:
Object.keys(obj).reduce((result, key) => {
// Add required properties
}, {});
Note: In JavaScript, an "associative array" is usually an object, not a true array. For actual arrays of objects, methods such as filter() or splice() are more appropriate.