Eliminar un atributo JSON [duplicar]
Esta pregunta ya tiene una respuesta aquí:
Si tengo un objeto JSON diga:
var myObj = {'test' : {'key1' : 'value', 'key2': 'value'}}
¿Puedo eliminar 'key1' para que se convierta en:
{'test' : {'key2': 'value'}}
257
Author: Josef Pfleger, 2009-08-02
2 answers
Simple:
delete myObj.test.key1;
429
Author: Josef Pfleger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-02 19:40:51
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-02 19:40:51
La respuesta seleccionada funcionaría durante el tiempo que conozca la clave en sí que desea eliminar, pero si debería ser realmente dinámica, necesitaría usar la notación [] en lugar de la notación de punto.
Por ejemplo:
var keyToDelete = "key1";
var myObj = {"test": {"key1": "value", "key2": "value"}}
//that will not work.
delete myObj.test.keyToDelete
En su lugar, usted tendría que utilizar:
delete myObj.test[keyToDelete];
Sustituya la notación de punto por la notación de [] para aquellos valores que desea evaluar antes de ser eliminados.
92
Author: praneetloke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-30 16:23:48
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-30 16:23:48