js 如何删除对象中的一个属性


Deleting Properties

The only way to actually remove a property from an object is to use thedelete operator; setting the property to undefined or null only remove the value associated with the property, but not the key.

var obj = {
    bar: 1,
    foo: 2,
    baz: 3
};
obj.bar = undefined;
obj.foo = null;
delete obj.baz;

for(var i in obj) {
    if (obj.hasOwnProperty(i)) {
        console.log(i, '' + obj[i]);
    }
}


The above outputs both bar undefined and foo null - only baz was removed and is therefore missing from the output.

阅读更多
文章标签: object
个人分类: JS
想对作者说点什么? 我来说一句

js移除属性

Sample Page!

panpanguo panpanguo

2011-01-06 22:39:00

阅读数:33490

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭