牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2011-10-17 投稿人: 浏览次数:13983


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.

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。