the for/in statement loops through the properties of the object.
JS to get the key to get a corresponding value in an object method: Obj.key
There are two ways to get the corresponding value of an object in JS based on dynamic key:
var key = "Name1"; var value = Obj[key];
var key = "Name1"; var value = eval ("obj.") +key);
JS Code:
var obj={"name": "Wjy", "age": +, "sex": "Female" };//defines an object var keys=[];//defines an array to accept the key var values=[];//definition an array to accept value for (var key in obj) { keys.push (Key); values.push (Obj[key]);//Acquired value alert (eval ("obj.") +key));//Print value value } alert (obj.name) in a loop ;//wjy alert ("KEYS IS :" +keys+ " AND VALUES IS :" +values); Keys is : name,age,sex and values is : wjy,26,female
1. When iterating through the properties of an object using the for In loop, the All properties on the prototype chain will be accessed :
object.prototype.bar = 10;// Modify object.prototype var obj={"name": "Wjy", "age": +, "sex": "female"};//define an object var keys=[];//defines an array to accept key var values=[];//to define an array to accept value for (var key in obj) { keys.push (key); values.push (Obj[key]);//Acquired value } alert ("KEYS IS :" + Keys+ " AND VALUES IS :" +values); //keys is : name,age,sex, bar and values is : wjy,26,female,10
2.
Function allpro (obj) { var keys=[]; var values=[]; for (var key in obj) { //only iterates over the properties of the object itself, not the attributes inherited from the prototype chain. if (Obj.hasownproperty (key) === true) { keys.push (key); values.push ( Obj[key]); } } alert ("KEYS IS :" +keys+ " and values is : "+valUES); } object.prototype.bar = 1;// Modify object.prototype var o={"name": "Wjy", "age": +, "sex": "female"};//define an object objects allpro (o); Keys is : name,age,sex and values is: wjy,26,female
Object objects do not have the length property, so they cannot be accessed directly through for (Var i=0;i<object.length;i++), and can be obtained by iterating over the length of the array that gets the key value to get the number of properties of the object itself.
keys.length;//"3"
For/in looping through the properties of an object