[英]My loop isn't separating the array items that don't match on the other array, how can I do that?
I`m trying to.push() the array items on both arrays, that don't match on each other and then assign it to the new array empty that I've created.. but it just iterate and return 60 items repeated.我正在尝试 push() 两个 arrays 上的数组项,它们彼此不匹配,然后将其分配给我创建的新数组空 .. 但它只是迭代并返回 60 个重复项。
What am I doing wrong?我究竟做错了什么?
Note: I'm still studying that subject, so I'm sorry if what I am doing is dumb haha注意:我还在研究那个主题,所以如果我做的是愚蠢的,我很抱歉哈哈
const bobsFollowers = ['James', 'Caleb', 'Rita', 'Samantha'];
const tinasFollowers = ['Maurice', 'Caleb', 'Samantha'];
const mutualFollowers = [];
const notMutualFollowers = [];
for (let i = 0; i < bobsFollowers.length; i++) {
for (let j = 0; j < tinasFollowers.length; j++) {
if (bobsFollowers[i] === tinasFollowers[j]) {
mutualFollowers.push(tinasFollowers[j]);
}
for (let x = 0; x < tinasFollowers.length && bobsFollowers.length; x++) {
if (bobsFollowers[i] !== tinasFollowers[j] && tinasFollowers[j] !== bobsFollowers[i])
notMutualFollowers.push(bobsFollowers[i], tinasFollowers[j]);
}
}
}
console.log(mutualFollowers);
console.log(notMutualFollowers);
I thing that is.我的意思是。 Ojs: I like define a const like currentValue just to make more sense and make understanding easier. Ojs:我喜欢定义一个像 currentValue 这样的 const 只是为了更有意义并且更容易理解。
for (let i = 0; i < bobsFollowers.length; i++) {
const currentName = bobsFollowers[i];
let isHaveSomeInArray = false;
for (let j = 0; j < tinasFollowers.length; j++) {
const currentComparation = tinasFollowers[j];
if (currentName === currentComparation) {
mutualFollowers.push(currentComparation);
isHaveSomeInArray = true;
}
}
if (!isHaveSomeInArray) {
notMutualFollowers.push(currentName);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.