Open In App

How to remove Objects from Associative Array in JavaScript ?

Last Updated : 13 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Approaches:

Approach 1: 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. 

javascript




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. 

javascript




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: 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:

Javascript




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' }

Approach 3: 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:

Javascript




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' }


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads