1、数组遍历去请求接口
toOpen(data) {
const _that = this
const newD = [...data] // data是一个数组对象
newD.forEach((item, index) => {
// 获取工作站下面的设备
wcGetStationApparatus({ wsid: item.wsid }).then((wcs) => {
if (wcs.code === 0 || wcs.code === '0') {
newD[index]['eqList'] = wcs.data
_that.dataSources = JSON.parse(JSON.stringify(newD))
}
console.log('3')
})
// 获取工作中心下面的班组
wcGetTeam({ wcid: item.wcid }).then((wcs) => {
if (wcs.code === 0 || wcs.code === '0') {
newD[index]['teamList'] = wcs.data
_that.dataSources = JSON.parse(JSON.stringify(newD))
}
console.log('3')
})
//获取工作中心下的工位
wcGetStation({ wcid: item.wcid }).then((res) => {
if (res.code === 0 || res.code === '0') {
newD[index]['stationList'] = res.data
_that.dataSources = JSON.parse(JSON.stringify(newD))//必须在回调里面去赋值,只有接口请求成功了之后,才能去赋值
}
console.log('3')
})
console.log('2')
})
console.log('1')
},
代码在浏览器运行
2/把若干数组按指定的字段名进行分组
function groupBy(list, propName) {
return list.reduce((acc, item) => {
const key = item[propName];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(item);
return acc;
}, {});
}
3/深度拷贝
function deepClone(obj) {
if (obj === null) return null;
if (typeof obj !== 'object') return obj;
if (obj instanceof Date) {
let date = new Date();
date.setTime(obj.getTime());
return date;
}
if (obj instanceof RegExp) {
let re = new RegExp(obj.source);
re.lastIndex = obj.lastIndex;
return re;
}
let newObj = new obj.constructor();
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = deepClone(obj[key]);
}
}
return newObj;
}