1尝蠕、
普通數(shù)組中對象的使用 angular.forEach(arr,function(value,key){})
舉例:
var array = { key1: "value1",key2: "value2", key3: "value3" };
angular.forEach(array,function(v,k){
console.log(k + "---->" + v);
})
由于function中參數(shù)依次為,當前遍歷元素牲迫,該元素下標;因此輸出結(jié)果為:
key1---->value1
key2---->value2
key3---->value3
舉例:
var objs =[{key:1},{key:2}];
angular.forEach(objs, function(item,index,array){
// item等價于array[index];
console.log(item.key+' = '+array[index].key);
});
該數(shù)組成員為對象類型,key--value
輸出結(jié)果為:
1 = 1
2 = 2
- objs:需要遍歷的集合
- item:遍歷時當前的數(shù)據(jù)
- index:遍歷時當前索引
- array:需要遍歷的集合苫幢,每次遍歷時都會把objs原樣的傳一次。
2垫挨、
Json數(shù)組用 array.forEcah(function(value,index){})
value表示數(shù)組每個單體元素韩肝,index為索引號
舉例:
var array = [
{
"name":"123"
},
{
"name":"234"
}
];
array.forEach(function(value,index){
console.log(index + "---->" + value.name)
})