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:

Javascript
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

Similar Reads

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

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

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

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