admin 管理员组文章数量: 1086019
var myJsonObj = {"employees":[{"name":"John", "lastName":"Doe", "age": 55},{"name":"Jane", "lastName":"Doe", "age":69}]};
How can I delete myJsonObj.eployees[1] ?
Thank you :)
var myJsonObj = {"employees":[{"name":"John", "lastName":"Doe", "age": 55},{"name":"Jane", "lastName":"Doe", "age":69}]};
How can I delete myJsonObj.eployees[1] ?
Thank you :)
Share Improve this question edited Dec 27, 2011 at 17:36 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Feb 15, 2010 at 7:15 jack moorejack moore 2,0595 gold badges26 silver badges22 bronze badges 1- 3 Note: There is no such thing as a JSON object, JSON is a data interchange format. That's just a regular object. – Guffa Commented Feb 15, 2010 at 8:01
3 Answers
Reset to default 5delete 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).
myJsonObj.employees.splice(1, 1);
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.
本文标签: arraysJavaScriptPrototypejs Delete property from JSON objectStack Overflow
版权声明:本文标题:arrays - JavaScriptPrototype.js: Delete property from JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744057022a2526034.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论