不太熟悉Javascript的Object对象的操作,查询了一些相关资料,做一个笔记。首先是关于获取对象的长度:
obj={"a":"1","b":"2","c":"3","d":"4","e":"5"};
Object.keys(obj).length;
判断对象是否包含某个“key”:
obj={"a":"1","b":"2","c":"3","d":"4","e":"5"};
//in方法
if('a' in obj){console.log("包含!");}
//hasOwnProperty方法
if(obj.hasOwnProperty('a')){console.log("包含!");}
删除对象的某个属性:
obj={"a":"1","b":"2","c":"3","d":"4"};
delete obj.a;
需要以非常优化的方式删除js对象属性,例如循环中对大量对象进行操作时,可以尝试将属性设置为undefined。由于它的性质, delete的性能比undefined简单的重新分配要慢得多,但是,采用undefined方法时,该属性不会从对象中删除。它的值已被擦除,但如果需要迭代对象,它仍然存在。
obj={"a":"1","b":"2","c":"3","d":"4"};
obj.a=undefined;