Cree una array asociativa que contenga un par clave-valor y la tarea es eliminar los objetos de una array asociativa mediante JavaScript. Podemos eliminar objetos de la array asociativa de JavaScript usando la palabra clave delete .
Enfoque: declarar una array asociativa que contenga objetos de par clave-valor. Luego use la palabra clave delete para eliminar los objetos de array de una array asociativa.
Ejemplo 1: este ejemplo utiliza la palabra clave delete para eliminar los objetos de la array asociativa.
<!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>
Producción:
Ejemplo 2: este ejemplo utiliza la palabra clave delete para eliminar los objetos de la array asociativa.
<!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>
Producción: