添加两个仅保留一次重复的数组-JavaScript
假设我们有两个这样的文字数组:
const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6];
我们需要编写一个JavaScript函数,该函数接受两个这样的数组,并返回一个删除了所有重复项的新数组(应该只出现一次)。
示例
让我们为该函数编写代码-
const arr1 = [2, 4, 5, 3, 7, 8, 9];
const arr2 = [1, 4, 5, 2, 3, 7, 6];
const mergeArrays = (first, second) => {
const { length: l1 } = first;
const { length: l2 } = second;
const res = [];
let temp = 0;
for(let i = 0; i < l1+l2; i++){
if(i >= l1){
temp = i - l1;
if(!res.includes(first[temp])){
res.push(first[temp]);
};
}else{
temp = i;
if(!res.includes(second[temp])){
res.push(second[temp]);
};
};
};
return res;
};
console.log(mergeArrays(arr1, arr2));输出结果
控制台中的输出:-
[ 1, 4, 5, 2, 3, 7, 6, 8, 9 ]
热门推荐
10 钢琴人祝福语女生简短
11 岗位考试顺利祝福语简短
12 关于老师蛋糕祝福语简短
13 娘家妈妈新婚祝福语简短
14 生日留言简短祝福语大全
15 祝福语诗意文案简短
16 校长退休文案祝福语简短
17 宝子生日祝福语简短
18 寒露祝福语毕业寄语简短