javascript - 从两个数组中返回一个不重复的值到一个数组中 - JavaScript ES5
问题描述
我正在尝试获得返回结果
["a", "to", "past", "adventure", "of"]
从
["a", "link", "to", "the", "past"]
["the", "adventure", "of", "link"]
我的返回结果是
["a", "link", "to", "the", "past", "the", "adventure", "of", "link"]
我尝试使用 .includes 方法,但我得到了更古怪的回报。我肯定错误地使用了语法。我不允许使用 .filter
我的代码
function difference(first, second) {
const newArray = [];
for (let i = 0; i < first.length; i++) {
if (first[i] !== second[i]) {
newArray.push(first[i]);
}
}
for (let j = 0; j < second.length; j++) {
if (second[j] !== first[j]) {
newArray.push(second[j]);
}
}
console.log(newArray);
return newArray;
}
解决方案
推荐阅读
- javascript - 如何合并 2 个相同长度的数组以获取彼此相邻的值?
- powershell-ise - Powershell多个文本框成一行?
- javascript - 没有这样的文件或目录。不和谐机器人
- javascript - 每次我想更新内容时,我是否需要更改我的网站的源代码?
- c# - 通过 c# 在 Windows 10 环境中安装 vosk api (speech to text)
- python - 如何将自动增量值分配给 Python3 中的 (k,v) 对 dict 的组合?
- javascript - 递归计数并找到给定值的总和
- ubuntu - 使用 NGINX 的不同域
- java - java - 为什么在Java中使用父类的构造方法创建对象时,我们不能创建自己的类型的子类对象
- r - 右 | 如何从 cv.glmnet 获得准确性