一、Js相关数组操作
- 数组去除相同的
let arr = [1,2,3]
let arr2 = [1,2,3,4,5]
// 输出arr3 = [4,5]
arr3 = arr.filter(v => {
return arr2.indexOf(v) === -1
});
- 数组添加数数据
let arr1 =[{ids:1,name:Emiley},{ids:2,name:bob},{ids:3,name:run}]
//输出 arr2 = [{id:1,ids:1,name:Emiley},{id:2,ids:2,name:bob},{id:3,ids:3,name:run}]
arr1.forEach(ele => {
if (ids.id) {
ele.ids= ele.id;
}
});
- 数组反转
reversePeople(array) {
let newArr = [];
for (let i = array.length - 1; i >= 0; i--) {
newArr[newArr.length] = array[i];
}
return newArr;
},
- 打乱数组排序
let arr = [1,2,3,4,5]
arr.sort(() => Math.random() - 0.5);
//随机输出一个排序打乱的数组
- 取数组的前几个
let arr = [1,2,3,4]
arr.slice(0,2)
//输出arr = [1,2]
- 数组扁平化
flatten(arr) {
return arr.join(',').split(',').map(function(item) {
return parseInt(item);
})
}
- 遍历对象取key值
let obj = res.data.record
let arrOne = [] //处理为数组
let finish = [] //完成
let remain = [] //未完成
let arrName = [] //镇的名字
//遍历对象
Object.getOwnPropertyNames(obj).forEach((key, index) => {
arrOne.push(obj[key])
arrName[index] = key
})
//遍历数组 组合数组
arrOne.forEach((key, index) => {
finish[index] = key.finish
remain[index] = key.remain
})
- 数组转换为对象,并且把数组处理成字符串
list(v) {
let arr = []
v.forEach((ele) => {
arr.push(ele.caseNo)
})
let arrOne = Object.assign({}, arr)
let ogj = Object.values(arrOne)
let string = JSON.stringify(ogj).replace(/\"/g, '').replace(/\[|]/g, '')
return string
}
- 将对象的数据拼接成数组的数据并且取到不同的值。
let arr = res.data.record.caseMap
let arrOne = []
//遍历了对象,添加图片路径进数组
Object.getOwnPropertyNames(arr).forEach((key, index) => {
arr[key].src = require(`@/assets/home/pic${index}.png`)
arrOne.push(arr[key])
})
let data = res.data.record
//业务类型
//(data.caseSourceMap)
let arr = []
let arrOne = []
Object.getOwnPropertyNames(data.caseSourceMap).forEach((key, index) => {
arr.push(key)
arrOne.push(data.caseSourceMap[key])
})
- 当数组不足9项的时候 补足 9项目。
var arr = [0,1,2]
var arrThe = [...arr,...ner Array(9-arr.length).fill(0)]
//输出 arrThe ==> [ 0,1,2,0,0,0,0,0,0,0]
11 .递归操作两颗树 比较两棵树 只留下 含有的 相同id 的树
// 递归
// 参数一 原本的树结构,参数2 id 组合成为的数组
filterMenu(menuList, menuCode) {
console.log(menuList, menuCode)
return menuList.filter(item => {
return menuCode.indexOf(item.id) > -1
}).map(item => {
item = Object.assign({}, item)
if (item.children) {
item.children = this.filterMenu(item.children, menuCode)
}
return item
})
},
12 .遍历一个对象 寻找其尾数相同的数组 并且将其拼合成一个全新的对象!
let wglist = {
nightPer2: "33.33%",
userAll3: 6,
nightPer1: "33.33%",
userAll2: 6,
nightPer4: "33.33%",
userAll5: 6,
nightPer3: "33.33%",
userAll4: 6,
userAll1: 6,
planDateStr1: "2021-06-08 00:00:00",
nightPer6: "33.33%",
nightPer5: "33.33%",
grdlCode: "3",
nightPer8: "33.33%",
nightPer7: "33.33%",
planDateStr6: "2021-06-13 00:00:00",
planDateStr7: "2021-06-14 00:00:00",
planDateStr8: "2021-06-15 00:00:00",
planDateStr2: "2021-06-09 00:00:00",
planDateStr3: "2021-06-10 00:00:00",
planDateStr4: "2021-06-11 00:00:00",
planDateStr5: "2021-06-12 00:00:00",
grdCode: "GRD6101150016",
dayPer5: "66.67%",
dayPer6: "66.67%",
dayPer7: "66.67%",
dayPer8: "66.67%",
dayPer1: "66.67%",
userAll7: 6,
dayPer2: "11.27%",
userAll6: 6,
dayPer3: "66.67%",
dayPer4: "66.67%",
userAll8: 6,
dayShift2: 4,
dayShift3: 4,
dayShift4: 4,
dayShift5: 4,
dayShift1: 4,
grdName: "油槐街道",
dayShift6: 4,
dayShift7: 4,
dayShift8: 4,
nightShift1: 2,
allPer8: "100.00%",
allPer7: "100.00%",
allPer6: "100.00%",
allPer5: "100.00%"
}
var resObj = new Array();
var pattern = /[0-9]$/;
Object.keys(wglist).forEach((key) => {
status = pattern.test(key);
if (status === 'true') {
var index = key.match(pattern)[0];
var val = wglist[key];
if (typeof (resObj[index]) === 'object') {
resObj[index][key] = val;
} else {
resObj[index] = new Object();
resObj[index][key] = val;
}
}
});
console.log('resObj', resObj)
网友评论