// 数组操作
export class AryAPI {
// 合并两个数组的相同项,只保留唯一项
static mergeArray (arr1, arr2) {
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length;) {
console.log('i,j', i, j, arr1[i], arr2[j], arr2.length)
if (arr1[i].key === arr2[j].key) {
arr2.splice(j, 1) // 利用splice函数删除元素,从第j个位置,截取长度为1的元素
} else { j++ }
}
}
for (let i = 0; i < arr2.length; i++) {
arr1.push(arr2[i])
}
return arr1
}
// 合并不同项
// 如[1,2,3,2],[2,3,4] 得到[3,4]
static merageDif (leftAry, rightAry) {
let delSameAry = []
// 删除rightAry中的相同项
for (let i = 0; i < leftAry.length; i++) {
for (let j = 0; j < rightAry.length; j++) {
if (leftAry[i].key === rightAry[j].key) {
delSameAry.push(leftAry[i])
rightAry.splice(j, 1) // 利用splice函数删除元素,从第j个位置,截取长度为1的元素
}
}
}
// 删除leftAry中的相同项
for (let i = 0; i < delSameAry.length; i++) {
for (let j = 0; j < leftAry.length; j++) {
if (delSameAry[i].key === leftAry[j].key) {
leftAry.splice(j, 1)
}
}
}
// 将剩余的合并
for (let i = 0; i < rightAry.length; i++) {
leftAry.push(rightAry[i])
}
return leftAry
}
// 删除相同项 从leftAry中删除与rightAry中的相同项
static removeSame (leftAry, rightAry) {
for (let i = 0; i < rightAry.length; i++) {
for (let j = 0; j < leftAry.length; j++) {
if (rightAry[i].key === leftAry[j].key) {
leftAry.splice(j, 1)
}
}
}
return leftAry
}
}