JS/jQuery 遍歷對象屬性


Javascript For/In 循環: 循環遍歷對象的屬性
 
var person={fname:"John",lname:"Doe",age:25};  

 

for (x in person)  

 

  {  

   txt=txt + person[x];  

  }  

結果:JohnDoe25  

 

jQuery jQuery.each() 遍歷對象屬性

 var arr = ["one", "two", "three", "four", "five"];  

var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };  

遍歷數組

var text = "Array ";  

jQuery.each(arr, function(i, val) {  

    text = text + " #Index:" + i + ":" + val;  

});  

console.log(text);  

//Array  #Index:0:one #Index:1:two #Index:2:three #Index:3:four #Index:4:five  

遍歷對象

text = "Object ";  

jQuery.each(obj, function(i, val) {  

    text = text + "Key:" + i + ", Value:" + val; 

});  

console.log(text);  

//Object Key:one, Value:1Key:two, Value:2Key:three, Value:3Key:four, Value:4Key:five, Value:5  

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM