JavaScript / Prototype.js: remove a property from a JSON object

var myJsonObj = {"employees":[{"name":"John", "lastName":"Doe", "age": 55},{"name":"Jane", "lastName":"Doe", "age":69}]}; 

How can I remove myJsonObj.eployees [1]?

Thanks:)

+4
source share
3 answers
 delete myJsonObj.employees[1]; 

However, this will retain the index of all other elements. If you want to re-order the index, you can also use this:

 // store current employee #0 var tmp = myJsonObj.employees.shift(); // remove old employee #1 myJsonObj.employees.shift(); // re-add employee #0 to the start of the array myJsonObj.employees.unshift(tmp); 

Or are you just using a solution for splicing Darin Dimitrova (see his answer below).

+5
source
 myJsonObj.employees.splice(1, 1); 
+2
source

Use delete :

 delete myJsonObj.employees[1] 

or set it to null

 myJsonObj.employees[1] = null; 

None of them will affect the indices of any elements following the element removed from the array.

0
source

Source: https://habr.com/ru/post/1301309/


All Articles