1.篩選需要的數(shù)組數(shù)據(jù)抒抬;
this.arr.filter(item => item.channel==3 || item.channel==6)
2.根據(jù)某個相同屬性值李丰,組合兩個數(shù)組
const combinedArray = array1.map(item => {
const item2 = array2.find(i => i.noticeId === item.dataId);
return { ...item, ...item2 };
});
2.根據(jù)某個相同屬性值捺僻,給a數(shù)組賦值b數(shù)組的某一個字段值
a.map(el=>{el.label=b.find(item=>item.channel==el.channel).label})
- 根據(jù)A數(shù)組的key篩選出B數(shù)組里對應(yīng)的數(shù)據(jù)
let checkList = ['110101202210242255', '612524196710250612']
this.familyList = [
{
idcard: '110101202210242255',
name: '張三'
},
{
idcard: '612524196710250612',
name: '李四'
},
{
idcard: '612524196710250699',
name: '王五'
}伺帘,
]
// 方法1:
let newList = this.familyList.filter(el => {
return checkList.some(curVal => curVal === el.idcard)
})
// 方法2:
let newList = this.familyList.filter(el => {
return checkList.find(item => {
return el.idcard == item
})
})
// 篩選非A數(shù)組的key的其它項:
let newList = this.familyList.filter(el => {
return !checkList.find(item => {
return el.idcard == item
})
})
4.將兩個數(shù)組組裝成對象數(shù)組
needArr = joinIds.map((i,index)=>{
return {
id: i,
name: joinNames[index]
}
})
- every/some校驗數(shù)組值是否符合條件(every-都符合條件,some-其中一個符合條件)
參數(shù)說明:
第一個參數(shù)為一個回調(diào)函數(shù),必傳厉亏,數(shù)組中的每一項都會遍歷執(zhí)行該函數(shù)。
currentValue:必傳烈和,當(dāng)前項的值
index:選傳爱只,當(dāng)前項的索引值
arr:選傳,當(dāng)前項所屬的數(shù)組對象
第二個參數(shù)thisValue為可選參數(shù)斥杜,回調(diào)函數(shù)中的this會指向該參數(shù)對象虱颗。
let flag = array.every(function(currentValue,index,arr), thisValue)
用法舉例:
let flag = resourceList.every((item) => {
return item.supplier.name && item.resource.name
})
6.精確匹配數(shù)組對象中的多個值
const users = [
{ name: 'Alice', age: 28 },
{ name: 'Bob', age: 30 },
{ name: 'Carol', age: 22 },
{ name: 'David', age: 25 }
];
// 定義要精確匹配的年齡數(shù)組
const ageValues = [25, 28];
// 使用 filter 方法篩選出年齡為指定值的用戶
const filteredUsers = users.filter(user => ageValues.includes(user.age));
console.log(filteredUsers);
//按順序做精確匹配
let arr = [
{ appName: "黨組織", moduleCode: 'dangzuzhi' },
{ appName: "黨委", moduleCode: 'dangwei' },
{ appName: "黨支部總數(shù)", moduleCode: 'dangzhibu' },
{ appName: "黨總支", moduleCode: 'dangzongzhi' },
];
//自定義需要篩選出來的數(shù)據(jù)key和順序
let newOrder = ['dangwei', 'dangzongzhi', 'dangzhibu', 'dangzuzhi'];
let newArr = newOrder.map(code => arr.find(item => item.moduleCode === code));
7.求和
let total = data.reduce((t, v) => {
return t + v.value
}, 0)
ES5
var a = [1,2,3];
var b = [1,2,2,4];
//并集(去重)
var union = a.concat(b.filter(function(v) {return a.indexOf(v) === -1 } )); // [1,2,3,4]
//交集
var intersection = a.filter(function(v){ return b.indexOf(v) > -1 }); // [1,2]
//差集
var difference = a.filter(function(v){ return b.indexOf(v) === -1 }); // [3]
ES6
let a = [1,2,3];
let b = [1,2,2,4];
let aSet = new Set(a);
let bSet = new Set(b);
//并集(去重)
let union = Array.from(new Set(a.concat(b))); // [1,2,3,4]
//交集
let intersection = Array.from(new Set(a.filter(v => bSet.has(v)))); // [2]
//差集
let differenceNew = Array.from(new Set(a.concat(b).filter(v => aSet.has(v) && !bSet.has(v))); // [3]
ES7
let a = [1,2,3];
let b = [1,2,2,4];
//并集(去重)
let union = a.concat(b.filter(v => !a.includes(v))); // [1,2,3,4]
//交集
let intersection = a.filter(v => b.includes(v)); // [1,2]
//差集
let difference = a.concat(b).filter(v => a.includes(v) && !b.includes(v)); // [3]
數(shù)組遍歷延遲執(zhí)行
for (let i = 0; i < fileIds.length; i++) {
setTimeout(() => {
let params = {
fileId: fileIds[i],
page: { page: 1, pageSize: 999 }
}
//每三秒用遍歷的item請求一次接口
service.queryFileOrNoticeReadInfoList(params).then(res => {
res.data.map(item=>{
if (item.memberId == memberId && item.readStatus == true)
readedFileIds.push(item.fileId)
})
}).catch(error => {
reject(error)
})
if (i >= fileIds.length -1) {
let totalReadIds = [...localReadIds, ...readedFileIds]
// 緩存已讀ids
localStorage.setItem('readedFileIds', JSON.stringify(totalReadIds))
resolve(totalReadIds)
}
}, 3000 * i)
}