[英]JavaScript/Prototype.js: Delete property from JSON object
var myJsonObj = {"employees":[{"name":"John", "lastName":"Doe", "age": 55},{"name":"Jane", "lastName":"Doe", "age":69}]};
How can I delete myJsonObj.eployees[1] ? 如何删除myJsonObj.eployees [1]?
Thank you :) 谢谢 :)
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). 或者您只使用Darin Dimitrov的拼接解决方案(请参阅下面的答案)。
myJsonObj.employees.splice(1, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.