一尘不染

如何从JavaScript对象中删除属性?

javascript

说我创建一个对象,如下所示:

let myObject = {
    "ircEvent": "PRIVMSG",
    "method": "newURI",
    "regex": "^http://.*"
};

删除该属性regex以使其最终成为new 的最佳方法是什么myObject

let myObject = {
    "ircEvent": "PRIVMSG",
    "method": "newURI"
};

阅读 380

收藏
2020-04-22

共1个答案

一尘不染

像这样:

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);
2020-04-22