Js determined whether the target object is empty
Determining whether a target object is empty, three kinds of determination methods presented herein:
1, the most common way of thinking, for ... in ... traversing the property, as was true "non-empty array"; otherwise, "empty array"
function judgeObj(Obj){
for(var attr in Obj){
return alert('非空对象')
}
return alert('空对象')
}
2. judged by JSON comes .stringify Method:
if(JSON.stringify(Obj) == '{}'){
console.log('空对象');
}
3.ES6 new method Object.keys ():
if(Object.keys(Obj).length > 0){//会转化一个数组
console.log('非空对象');
}
Whether one of the object attributes determined JavaScript
Is there a common way of summary judgment object attributes, different scenarios to be used in different ways.
A dot (.) Or brackets ([])
Property value may be acquired by the object point or brackets, if the attribute does not exist on the object is returned undefined. Of course, the "absent" means that the object itself and not present on the prototype chain, the chain has the property if the prototype, the prototype chain attribute value is returned.
// 创建对象
let test = {name : 'lei'}
// 获取对象的自身的属性
test.name //"lei"
test["name"] //"lei"
// 获取不存在的属性
test.age //undefined
// 获取原型上的属性
test["toString"] //toString() { [native code] }
// 新增一个值为undefined的属性
test.un = undefined
test.un //undefined 不能用在属性值存在,但可能为 undefined的场景
Therefore, depending on whether we can obj.x undefined return value to determine whether there obj x property. This way is very simple, that is, limitations: You can not use in the presence of an attribute value of x, but may be undefined scene. in operator can solve this problem
Programming sixth year, to share with you some of the learning method, combat development need to pay attention to detail. 767-273-102 autumn dress. From the zero-based front-end to learn how to start. To see how the predecessors proudly forward in the programming world! Constantly updated with the latest tutorials and learning methods (web front-end system to learn the route, detailed front-end project combat instructional videos), I had wanted to learn web front-end, or change jobs, or college students, as well as work want to upgrade their skills, is studying small partners are welcome to join. We will walk together with the front tip of the tip
Two, in the operator
If the specified property of the specified object or prototype chain in the operator returns true.
// 创建对象
let test = {name : 'lei'}
'name' in test //true
'un' in test //true
'toString' in test //true
'age' in test //false
As can be seen in the example, the attribute value is undefined may be judged normal. The limitations of this approach is that the property can not distinguish between itself and the prototype chain, when only need to determine their own property exists in this way does not apply. Then need hasOwnProperty ()
三、hasOwnProperty()
test.hasOwnProperty('name') //true 自身属性
test.hasOwnProperty('age') //false 不存在
test.hasOwnProperty('toString') //false 原型链上属性
It can be seen only when the existence of the property itself, will return true. Analyzing only applicable to own attribute scene.
to sum up
Three methods have advantages and disadvantages, different scenarios in different ways, and sometimes requires the use of combination, such as traversing their property and they will put to use in ··· for ··· and hasOwnProperty () combined.