views:

763

answers:

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

How can I delete myJsonObj.eployees[1] ?

Thank you :)

+1  A: 
delete myJsonObj.employees[1];

However, this will keep the index of all the other elements. If you want to re-order the index, too, you could 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 you use simply Darin Dimitrov's splice solution (see his answer below).

Boldewyn
Thanks Boldewyn! :)
jack moore
You're welcome.
Boldewyn
A: 

Use delete:

delete myJsonObj.employees[1] 

or set it to null

myJsonObj.employees[1] = null;

Neither will affect the indices of any elements following the element deleted from an array.

outis
+1  A: 
myJsonObj.employees.splice(1, 1);
Darin Dimitrov
Wow I like it. Thanks!
jack moore