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 the delete keyword.
Approach: 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
<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> |
Output:
Example 2: This example uses the delete keyword to remove the objects from the associative array.
javascript
<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> |
Output:
Please Login to comment...