要从对象中删除属性(改变对象),可以按如下方式操作:
delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];
演示
var myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
delete myObject.regex;
console.log(myObject);
对于任何有兴趣阅读更多相关信息的人,Stack Overflow用户kangax在他们的博客上写了一篇关于该声明的非常深入的博客文章,理解删除。强烈推荐。delete
如果您想要一个包含原始键(某些键除外)的新对象,则可以使用解构。
演示
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
// assign the key regex to the variable _ indicating it will be unused
const {regex: _, ...newObj} = myObject;
console.log(newObj); // has no 'regex' key
console.log(myObject); // remains unchanged