一暑始、Js相關(guān)數(shù)組操作
- 數(shù)組去除相同的
let arr = [1,2,3]
let arr2 = [1,2,3,4,5]
// 輸出arr3 = [4,5]
arr3 = arr.filter(v => {
return arr2.indexOf(v) === -1
});
- 數(shù)組添加數(shù)數(shù)據(jù)
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;
}
});
- 數(shù)組反轉(zhuǎn)
reversePeople(array) {
let newArr = [];
for (let i = array.length - 1; i >= 0; i--) {
newArr[newArr.length] = array[i];
}
return newArr;
},
- 打亂數(shù)組排序
let arr = [1,2,3,4,5]
arr.sort(() => Math.random() - 0.5);
//隨機(jī)輸出一個(gè)排序打亂的數(shù)組
- 取數(shù)組的前幾個(gè)
let arr = [1,2,3,4]
arr.slice(0,2)
//輸出arr = [1,2]
- 數(shù)組扁平化
flatten(arr) {
return arr.join(',').split(',').map(function(item) {
return parseInt(item);
})
}
- 遍歷對(duì)象取key值
let obj = res.data.record
let arrOne = [] //處理為數(shù)組
let finish = [] //完成
let remain = [] //未完成
let arrName = [] //鎮(zhèn)的名字
//遍歷對(duì)象
Object.getOwnPropertyNames(obj).forEach((key, index) => {
arrOne.push(obj[key])
arrName[index] = key
})
//遍歷數(shù)組 組合數(shù)組
arrOne.forEach((key, index) => {
finish[index] = key.finish
remain[index] = key.remain
})
- 數(shù)組轉(zhuǎn)換為對(duì)象弥喉,并且把數(shù)組處理成字符串
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
}
- 將對(duì)象的數(shù)據(jù)拼接成數(shù)組的數(shù)據(jù)并且取到不同的值。
let arr = res.data.record.caseMap
let arrOne = []
//遍歷了對(duì)象蚂四,添加圖片路徑進(jìn)數(shù)組
Object.getOwnPropertyNames(arr).forEach((key, index) => {
arr[key].src = require(`@/assets/home/pic${index}.png`)
arrOne.push(arr[key])
})
let data = res.data.record
//業(yè)務(wù)類型
//(data.caseSourceMap)
let arr = []
let arrOne = []
Object.getOwnPropertyNames(data.caseSourceMap).forEach((key, index) => {
arr.push(key)
arrOne.push(data.caseSourceMap[key])
})
- 當(dāng)數(shù)組不足9項(xiàng)的時(shí)候 補(bǔ)足 9項(xiàng)目。
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 的樹
// 遞歸
// 參數(shù)一 原本的樹結(jié)構(gòu),參數(shù)2 id 組合成為的數(shù)組
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 .遍歷一個(gè)對(duì)象 尋找其尾數(shù)相同的數(shù)組 并且將其拼合成一個(gè)全新的對(duì)象谱醇!
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)