今天就跟大家聊聊有关如何在Vue中使用foreach遍历数组,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
在js中遍历数组
//定义班次详细数组 var bcglxiangxiList = new Array(); //定义班次详细对象 var bcxiangxi = {}; //循环传递的参数 bcglXiangXiListParam.forEach(element => { bcxiangxi.xh = element.xh; bcxiangxi.bcbh = data.bcbh; bcxiangxi.dkkssj = element.sjfw[0]; bcxiangxi.dkjssj = element.sjfw[1]; bcxiangxi.ts = element.ts; bcxiangxi.dkdd = element.dkdd; bcxiangxi.zxjxljsj = element.jxsjfw[0]; bcxiangxi.zdjxljsj = element.jxsjfw[1]; //将班次详细对象存进班次详细数组 bcglxiangxiList.push(bcxiangxi); });
在vue中遍历数组
var bcglxiangxiList = new Array(); var bcxiangxi = {}; debugger; if ( response.data.bcglXiangXiList != null && response.data.bcglXiangXiList.length > 0 ) { console.log(response.data.bcglXiangXiList); response.data.bcglXiangXiList.forEach((item, index) => { console.log(item); bcxiangxi.xh = item.xh; bcxiangxi.bcbh = item.bcbh; //debugger bcxiangxi.sjfw = new Array(); bcxiangxi.sjfw[0] = item.dkkssj; bcxiangxi.sjfw[1] = item.dkjssj; bcxiangxi.ts = item.ts; bcxiangxi.dkdd = item.dkdd; bcxiangxi.jxsjfw = new Array(); bcxiangxi.jxsjfw[0] = item.zxjxljsj; bcxiangxi.jxsjfw[1] = item.zdjxljsj; bcglxiangxiList.push(bcxiangxi); }); }
可以看到遍历的方式是一样的,在js中一样可以用两个参数的遍历方式
//定义班次详细数组 var bcglxiangxiList = new Array(); //定义班次详细对象 var bcxiangxi = {}; //循环传递的参数 bcglXiangXiListParam.forEach((element,index) => { bcxiangxi.xh = element.xh; bcxiangxi.bcbh = data.bcbh; bcxiangxi.dkkssj = element.sjfw[0]; bcxiangxi.dkjssj = element.sjfw[1]; bcxiangxi.ts = element.ts; bcxiangxi.dkdd = element.dkdd; bcxiangxi.zxjxljsj = element.jxsjfw[0]; bcxiangxi.zdjxljsj = element.jxsjfw[1]; //将班次详细对象存进班次详细数组 bcglxiangxiList.push(bcxiangxi); });
vue forEach循环使用
//data为集合 data.forEach(function(item, index) { //item 就是当日按循环到的对象 //index是循环的索引,从0开始 })