Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

javascript - 如何从JavaScript对象中删除属性?(How do I remove a property from a JavaScript object?)

Say I create an object as follows:

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

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

What is the best way to remove the property regex to end up with new myObject as follows?

(删除属性regex以产生新的myObject的最佳方法是什么?)

var myObject = {
    "ircEvent": "PRIVMSG",
    "method": "newURI"
};
  ask by johnstok translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like this:

(像这样:)

delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];

Demo

(演示版)

 var myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; delete myObject.regex; console.log(myObject); 

For anyone interested in reading more about it, Stack Overflow user kangax has written an incredibly in-depth blog post about the delete statement on their blog, Understanding delete .

(对于任何有兴趣阅读更多内容的人,Stack Overflow用户kangax都写了一篇非常深入的博客文章,关于其博客上的delete语句,“ 了解删除” 。)

It is highly recommended.

(强烈建议。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...