在JavaScript中找到两个Number数组中的偏差
我们需要编写一个JavaScript函数,该函数接受Number数组,并从两个都不通用的数组中返回元素。
例如,如果两个数组是-
const arr1 = [2, 4, 2, 4, 6, 4, 3]; const arr2 = [4, 2, 5, 12, 4, 1, 3, 34];
输出结果
那么输出应该是-
const output = [ 6, 5, 12, 1, 34 ]
示例
为此的代码将是-
const arr1 = [2, 4, 2, 4, 6, 4, 3];
const arr2 = [4, 2, 5, 12, 4, 1, 3, 34];
const deviations = (first, second) => {
const res = [];
for(let i = 0; i < first.length; i++){
if(second.indexOf(first[i]) === -1){
res.push(first[i]);
}
};
for(let j = 0; j < second.length; j++){
if(first.indexOf(second[j]) === -1){
res.push(second[j]);
};
};
return res;
};
console.log(deviations(arr1, arr2));输出结果
控制台中的输出-
[6, 5, 12, 1, 34 ]
热门推荐
6 甄嬛传祝福语简短
10 祝福语勤奋大全简短英语
11 七一祝福语明信片文案简短
12 分手快乐祝福语简短
13 姥爷生日祝福语简短精辟
14 毕业结婚祝福语简短精辟
15 五二零朋友祝福语大全简短
16 考试祝福语简短给对象
17 祝女儿出嫁简短祝福语
18 员工对同事祝福语简短