For/in looping through the properties of an object

Source: Internet
Author: User
Tags hasownproperty

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.