Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to remove Objects from Associative Array in JavaScript ?

  • Last Updated : 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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :