for(var i in items) 和 for(var i;i<items.length;i++) 区别
前者循环的是属性,后者循环的才是数组。 若项目中对数组属性进行了扩展,那切记不能使用前者,否则在循环数组时扩展的函数体也会被当做数据返回。 var data = { p1:1, p2:"b" }; for (var i in data){ alert(eval("data." + i)); } for(var i in items) 慎用 尤其在需要兼容IE8时。
eg:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var data= [ { "LAY_TABLE_INDEX": 0, "createTime": "2019-11-20 17:13:11", "createUser": "8", "id": 92, "mergeFlag": 0, "onlineFlag": 0, "querySql": "select u.user_login_name as userLoginName,u.user_name as userName,u.user_status as userStatus,r.role_name as roleName,u.creator,u.create_time as createTime,u.modifier,u.update_time as updateTime from system_user u left join system_user_role ur on ur.user_id=u.user_id left join system_role r on ur.role_id=r.role_id", "reportName": "用户统计报表", "reportType": "", "tableHeaderLevel": 3 }, { "LAY_TABLE_INDEX": 1, "createTime": "2019-11-20 17:13:11", "createUser": "8", "id": 93, "mergeFlag": 1, "querySql": "select section_owner_name as owner,section_name as roadsubsection,toll_station_name as tollstation,lanetype as lanetype,cardtype as cardtype,type as inletandoutlet,bus1 as passenger1,bus2 as passenger2,bus3 as passenger3,bus4 as passenger4,bus_traffic as subtotal1,truck1,truck2,truck3,truck4,truck5,truck_traffic as subtotal2,traffic as total from st_etc_day_all_sum_dm_20191028", "reportBore": "fdsafd", "reportName": "ETC使用率报表修改", "reportType": "", "tableHeaderLevel": 4 }, { "LAY_TABLE_INDEX": 2, "createTime": "2019-12-05 14:51:09", "createUser": "8", "id": 104, "mergeFlag": 0, "onlineFlag": 0, "querySql": ",select section_owner_name ,section_name as roadsubsection,toll_station_name as tollstation,bus1 as passenger1,bus2 as passenger2,bus3 as passenger3,bus4 as passenger4,bus_traffic as subtotal1,traffic as total from st_etc_day_all_sum_dm_20191028", "reportBore": "fdgfds", "reportName": "ETC使用率报表96", "reportType": "1", "tableHeaderLevel": 3 }, { "LAY_TABLE_INDEX": 3, "createTime": "2019-12-05 14:59:14", "createUser": "8", "id": 106, "mergeFlag": 0, "onlineFlag": 0, "querySql": "select section_owner_name ,section_name as roadsubsection,toll_station_name as tollstation,bus1 as passenger1,bus2 as passenger2,bus3 as passenger3,bus4 as passenger4,bus_traffic as subtotal1,traffic as total from st_etc_day_all_sum_dm_20191028", "reportBore": "dsddfdf", "reportName": "ETC使用率报表修改", "reportType": "1", "tableHeaderLevel": 3 }, { "LAY_TABLE_INDEX": 4, "accountPerType": 1, "createTime": "2019-12-19 10:04:27", "createUser": "8", "id": 107, "mergeFlag": 1, "onlineFlag": 0, "querySql": "select id,type,mjnumber,source,sum,secussssum,secusslv,descf from hsop_test order by id", "reportBore": "测试合并", "reportName": "测试合并", "reportType": "1", "tableHeaderLevel": 2 }, { "LAY_TABLE_INDEX": 5, "accountPerType": 1, "createTime": "2019-12-19 10:56:44", "createUser": "8", "id": 108, "mergeFlag": 1, "onlineFlag": 0, "querySql": "select id,type,mjnumber,source,sum,secussssum,secusslv,descf from hsop_test order by id", "reportBore": "dddd", "reportName": "wenjinhong", "reportType": "1", "tableHeaderLevel": 2 } ] for (var item in data) { console.log(data[item]); console.log(data[item].id); }
结果: