简体   繁体   English

JavaScript / Prototype.js:从JSON对象中删除属性

[英]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);

Use delete : 使用删除

delete myJsonObj.employees[1] 

or set it to null 或者将其设置为null

myJsonObj.employees[1] = null;

Neither will affect the indices of any elements following the element deleted from an array. 也不会影响从数组中删除元素后的任何元素的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM