简体   繁体   English

我的循环没有分隔与另一个数组不匹配的数组项,我该怎么做?

[英]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.

相关问题 如何将数组中的项目与对象中的项目匹配? - How can I match items in my array with items in my object? 如何在Azure表存储中查找与值数组不匹配的记录? - How do I find records in Azure table storage that don't match an array of values? 我的数组中没有任何数据,只有一个,就是这样,我不明白为什么? - My array isn't taking any data in it, only one and that's it, and I don't understand why? 如何重写此Firebase函数,以使返回的数组不为空? - How do I rewrite this Firebase function so my returned array isn't empty? 所以我试图在一个数组中获取我的 axios 响应,但我不知道该怎么做 - So i'm trying to get my axios response in an array and I don't know how to do it 我不知道如何解决我的反向数组/字符串 - I don't know how to fix my reverse array/string 为什么数组的相应部分在我的代码中不匹配? - Why don't the corresponding parts of the array match up in my code? 带角度-不显示基于其他阵列项目的阵列项目进行Ng重复 - With Angular - Don't Display Array Items based on other Array's Items for Ng-Repeat 如何使用 for 循环将项目推送到数组? - How do I push items to array with a for loop? 不明白为什么我的 for 循环代码不起作用 - Don't understand why my for loop code isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM