How to remove Objects from Associative Array in JavaScript ?
Create an associative array containing key-value pair and the task is to remove the objects from an associative array using JavaScript. We can remove objects from JavaScript associative array using delete keyword.
Approach: Declare an associative array containing key-value pair objects. Then use delete keyword to delete the array objects from an associative array.
Example 1: This example uses delete keyword to remove the objects from associative array.
<!DOCTYPE html> <html> <head> <title> How to remove Objects from Associative Array in JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> How to remove Objects from Associative Array in JavaScript ? </h3> <script> // JavaScript code to remove // objects from associative array function deleteObjects(){ // Declaring an associative // array of objects var arr = new Object(); // Adding objects in array arr['key'] = 'Value'; arr['geeks'] = 'GeeksforGeeks'; arr['name'] = 'Rajnish'; // Checking object exist or not document.write(arr['name'] + '</br>'); // Removing object from // associative array delete arr['name']; // It gives result as undefined // as object is deleted document.write(arr['name'] + '</br>'); } // Calling function deleteObjects(); </script> </body> </html> |
Output:

Example 2: This example uses delete keyword to remove the objects from associative array.
<!DOCTYPE html> <html> <head> <title> How to remove Objects from Associative Array in JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> How to remove Objects from Associative Array in JavaScript ? </h3> <script> // JavaScript code to remove // objects from associative array function deleteObjects(){ // Declaring an associative // array of objects var arr = new Object(); // Adding objects in array arr['key'] = 'Value'; arr['geeks'] = 'GeeksforGeeks'; arr['name'] = 'Rajnish'; // Checking object exist or not document.write(arr['geeks'] + '</br>'); // Removing object from // associative array delete arr.geeks; // It gives result as undefined // as object is deleted document.write(arr['geeks'] + '</br>'); } // Calling function deleteObjects(); </script> </body> </html> |
Output:

Recommended Posts:
- How to remove duplicates from an array of objects using JavaScript ?
- How to remove a key and its value from an associative array in PHP ?
- How to get a list of associative array keys in JavaScript ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- Max/Min value of an attribute in an array of objects in JavaScript
- JavaScript | length of string and array objects
- Sort array of objects by string property value in JavaScript
- How to remove an element from an array in JavaScript?
- JavaScript | Remove the last item from an array
- JavaScript | Remove empty elements from an array
- How to remove multiple elements from array in JavaScript ?
- Extract unique objects by attribute from array of objects.
- How to get all unique values (remove duplicates) in a JavaScript array?
- How to loop through an associative array and get the key in PHP?
- How to check an array is associative or sequential in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



