foreach、for、for in循环方式

$(function(){
var data1 =
[
{
id:'126652',
no: '1',
longitude: '103.94292',
latitude: '30.620123',
distance:'253'
}, {
id:'122855',
no: '2',
longitude: '103.94292',
latitude: '30.620123',
distance:'258.25'
}, {
id:'1285569',
no: '3',
longitude: '103.94292',
latitude: '30.620123',
distance:'253'
}, {
id:'1222555',
no: '4',
longitude: '103.94292',
latitude: '30.620123',
distance:'253'
}]

//遍历数组而设计
data1.forEach(function (val,index,arr) {
console.log(val) //{id: "126652", no: "1", longitude: "103.94292", latitude: "30.620123", distance: "253"}
console.log(val.no);//1 2 3 4
console.log(index);//0 1 2 3
console.log(arr);//整个数组
})
var data2 = {a:'1',b:'2',c:'3',d:'4'}
//循环对象而设计
for(let i in data2){
console.log(i)//a b c d
console.log(data2[i])//1 2 3 4
}
var data3 = [1,2,3,4]
//最原始遍历方式
for(var i=0;i<data3.length;i++){
console.log(data3[i])//1 2 3 4
}
})




//修改某一条对象数据值
var stooges = [
 2   {label: 1, value: 1, placeCode: 'sh', stock_code: 40},
 3   {label: 1, value: 1, placeCode: 'sh', stock_code: 50},
 4   {label: 1, value: 1, placeCode: 'sz', stock_code: 60, status: 1},
 5   {label: 1, value: 1, placeCode: 'sz', stock_code: 39, status: 1}
 6 ];
 7 
 8 for (var i = 0; i < stooges.length; i++) {
 9   var item = stooges[i];
10   if (item.placeCode == "sh") {
11     item.placeCode = "沪";
12   }
13   if (item.placeCode == "sz") {
14     item.placeCode = "深";
15   }
16 }
17 写个for循环遍历一下,从第一个对象开始,发现对应的placeCode是sh则更新成文字沪,发现对应的placeCode是sz则更新成文字深,一直到最后一个对象
posted on 2018-11-20 16:40  忆小样  阅读(410)  评论(0编辑  收藏  举报