迴圈遍歷物件的屬性

2022-07-25 10:00:15 字數 732 閱讀 9980

123

4點選下面的按鈕,迴圈遍歷物件 "person" 的屬性。

5點選這裡67

82324

上面**執行的結果是:fname:bill lname:gates age:56

有人不明白17行,txt = 後面為什麼還要加txt ,如果去掉的話就只會正剩下最後乙個屬性。

其實 txt = txt + x+':'+ person[x] +' '; 的意思就是將值累加起來,我們不妨拆開迴圈的步驟來看。

第一次迴圈x=fname person[x]為bill, txt ="" 則 txt = "" +"fname" +":" +"bill", txt=「fname:bill"

第二次迴圈x= lname person[x]為gates, txt="fname:bill" 則 txt = "fname:bill"+"lname "+":" + "gates", txt="fname:bill iname:gates"

第三次迴圈x=age person[x]為56,txt ="fname:bill iname:gates",則txt="fname:bill iname:gates" + "age" + ":" +"56", txt="fname:bill lname:gates age:56", 迴圈結束。

其實  txt = txt + x+':'+ person[x] +' '; 可以直接寫成 txt += x+':'+ person[x] +'  ';

Javascript 遍歷物件的屬性

我們可以用 for in 來遍歷物件的屬性。function computer brand,price var mycomputer new computer acer 500 for var prop in mycomputer 上面的 中,computer有兩個屬性,brand 和 price....

lodash 物件 迴圈遍歷 排序

1.foreach collection,iteratee identity thisarg 遍歷 foreach 22,33,11,55 function value foreach 22,33,11,55 function value,index 2.sortby collection,iter...

Java遍歷物件所有屬性

要獲取物件的所有屬性可以使用getdeclaredfields 方法會返回乙個field陣列 遍歷這個陣列幾個遍歷所有屬性 注意使用這個方法會丟擲4個異常 然後根據屬性的型別選擇執行對應的內容 public static void eachproperties object model throws...