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.



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example 2: This example uses delete keyword to remove the objects from associative array.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.