How to remove Objects from Associative Array in JavaScript ? - GeeksforGeeks

16 Oct, 2019
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:
javascript-array
javascript-object
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript