_.allKeys(obj) array(say var arr) 引用对象时的意外行为:obj.arr[0],它 returns 未定义
Unexpected behaviour when _.allKeys(obj) array(say var arr) is referred to object :obj.arr[0], it returns undefined
对于那些不知道 _.allKeys(obj) 做什么的人,这里有一个片段
var allKeys = function(obj){
var key=[];
for(var pname in obj){
key.push(pname);
}
return key;
};`
所以它 returns 传递给它的对象的 property/method 名称数组。
//Say I have an object obj.
var obj={firstname:"John", lastname:"Adams"};
var arr=allKeys(obj); //stores the returned array of property names into arr.
for(var i;i<arr.length;i++){
console.log("Property name: "+arr[i]); //this detects the propertyname
console.log("Value name"+obj.arr[i]); //But when its referred to the object it does not return its value,why so ?
console.log("------");
}
这应该给我:
Property name: firstname
Value name: John
------
Property name: lastname
Value name: Adams
------
相反,它给了我:
Property name: firstname
Value name: undefined
------
Property name: lastname
Value name: undefined
------
知道为什么会这样吗?
你应该通过这种方式获得价值。 .
和[]
都是用来获取某个key的值,但是当key是变量的时候,需要[]
,而不是.
。
如果您只想显示键和值,则无需采用那种复杂的方式。
var obj={firstname:"John", lastname:"Adams"};
for(let key in obj){
console.log("Property name: "+key); //this detects the propertyname
console.log("Value name"+obj[key]);
console.log("------");
}
这就是您所需要的。
这会给您带来与预期相同的结果
var allKeys = function(obj){
var key=[];
for(var pname in obj){
key.push(pname);
}
return key;
};
var obj={firstname:"John", lastname:"Adams"};
var arr=allKeys(obj); //stores the returned array of property names into arr.
for(var i=0;i<arr.length;i++){
console.log("Property name: "+arr[i]); //this detects the propertyname
console.log("Value name: "+obj[arr[i]]); // I used BRACKET NOTATION
console.log("------");
}
希望这对您有所帮助:)
对于那些不知道 _.allKeys(obj) 做什么的人,这里有一个片段
var allKeys = function(obj){
var key=[];
for(var pname in obj){
key.push(pname);
}
return key;
};`
所以它 returns 传递给它的对象的 property/method 名称数组。
//Say I have an object obj.
var obj={firstname:"John", lastname:"Adams"};
var arr=allKeys(obj); //stores the returned array of property names into arr.
for(var i;i<arr.length;i++){
console.log("Property name: "+arr[i]); //this detects the propertyname
console.log("Value name"+obj.arr[i]); //But when its referred to the object it does not return its value,why so ?
console.log("------");
}
这应该给我:
Property name: firstname
Value name: John
------
Property name: lastname
Value name: Adams
------
相反,它给了我:
Property name: firstname
Value name: undefined
------
Property name: lastname
Value name: undefined
------
知道为什么会这样吗?
你应该通过这种方式获得价值。 .
和[]
都是用来获取某个key的值,但是当key是变量的时候,需要[]
,而不是.
。
如果您只想显示键和值,则无需采用那种复杂的方式。
var obj={firstname:"John", lastname:"Adams"};
for(let key in obj){
console.log("Property name: "+key); //this detects the propertyname
console.log("Value name"+obj[key]);
console.log("------");
}
这就是您所需要的。
这会给您带来与预期相同的结果
var allKeys = function(obj){
var key=[];
for(var pname in obj){
key.push(pname);
}
return key;
};
var obj={firstname:"John", lastname:"Adams"};
var arr=allKeys(obj); //stores the returned array of property names into arr.
for(var i=0;i<arr.length;i++){
console.log("Property name: "+arr[i]); //this detects the propertyname
console.log("Value name: "+obj[arr[i]]); // I used BRACKET NOTATION
console.log("------");
}
希望这对您有所帮助:)