2

Hi I was wondering how I could get bobs and tina same followers into an empty array mutualfollowers. I am getting output Both followers have undefined. Seem like the name is not passing through. Please advise.

    let bobsFollowers = ['grey', 'mary', 'james', 'ash'];
    let tinasFollowers = ['grey', 'mary', 'rex'];
    let mutualFollowers = [];
    
    for(let i = 0; i<bobsFollowers.length; i++){
      for(let k = 0; k<tinasFollowers.length; k++){
        if (bobsFollowers[i] === tinasFollowers[k]) {
          tinasFollowers.push(mutualFollowers);
          console.log('Both followers have ' + mutualFollowers[k]);
        }
      }
    }
Kim
  • 489
  • 5
  • 13
  • 1
    `let mutualFollowers = bobsFollowers.filter(f => tinasFollowers.includes(f));` should do it. –  Jun 15 '21 at 08:05
  • You are pushing into the wrong array. Push in mutualFollowers mutualFollowers.push(tinasFollowers[k]) . – Tushar Shahi Jun 15 '21 at 08:06
  • Does this answer your question? [Simplest code for array intersection in javascript](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – Hans-Martin Mosner Jun 15 '21 at 08:10

3 Answers3

3

You need to push to mutualFollowers. And for making the loops a bit more performat, you could leave the inner loop on found.

let bobsFollowers = ['grey', 'mary', 'james', 'ash'];
let tinasFollowers = ['grey', 'mary', 'rex'];
let mutualFollowers = [];

for (let i = 0; i < bobsFollowers.length; i++) {
  for (let k = 0; k < tinasFollowers.length; k++) {
    if (bobsFollowers[i] === tinasFollowers[k]) {
      mutualFollowers.push(bobsFollowers[i]);
      break;
    }
  }
}

console.log(mutualFollowers);

To get just the common items, you could take a Set and filter with Set#has.

const
    bobsFollowers = ['grey', 'mary', 'james', 'ash'],
    tinasFollowers = ['grey', 'mary', 'rex'],
    common = bobsFollowers.filter(
        Set.prototype.has,
        new Set(tinasFollowers)
    );

console.log(common);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    I am getting output as 'Both followers have undefined' twice – Kim Jun 15 '21 at 08:09
  • it looks like you add `undefined` to the array. maybe the index is wrong. – Nina Scholz Jun 15 '21 at 08:10
  • 1
    Nina, could you please elaborate more about how it works with the filter and set together? I don't understand why you pass ```Set.prototype.has``` and ```new Set``` in the filter() function. – ikhvjs Jun 15 '21 at 09:30
  • the prototype delivers the methos and the set is `thisArg` of the filter method. please ahave a look here: [`Array#filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Nina Scholz Jun 15 '21 at 09:32
  • Thanks. What I understand is actually ```thisArg``` is applied to the prototype method and it would be like ```this.has``` as a callbackFunc in the filter and this is referred to the ```new Set``` – ikhvjs Jun 15 '21 at 09:51
  • 1
    yes. if `filter` does not have `thisArg`, you could bind it and take the returned function, like `Set.prototype.has.bind(new Set(tinasFollowers))`. – Nina Scholz Jun 15 '21 at 09:53
2

The problem in your code is that you are pushing the mutualFollowers empty array into tinasFollowers one.

You need to push the actual element, so replace this:

tinasFollowers.push(mutualFollowers);
// You are pushing the "mutualFollowers" array as an item into the "tinasFollowers" array

With this:

mutualFollowers.push(tinasFollowers[k]);
// You are pushing the "tinasFollowers[k]" item (the one you are checking for equality to the bobsFollowers[i] item) into the "mutualFollowers" array

There's a much more simple way to do what you want to do though:

let bobsFollowers = ['grey', 'mary', 'james', 'ash'];
let tinasFollowers = ['grey', 'mary', 'rex'];
let mutualFollowers = bobsFollowers.filter(value => tinasFollowers.includes(value));

Which is a way of saying: "add all elements in bobFollowers, which are in tinasFollowers"

PS: what you are doing is called a "two-array intersection" (your result is the elements that intersect on both arrays), and there are better and more optimized algorithms around, if you want to search more

Jcl
  • 27,696
  • 5
  • 61
  • 92
0

hope this piece of code helps, it is just same as yours, just modified a bit. Using the JS filter method is much better for this task.

  let bobsFollowers = ['grey', 'mary', 'james', 'ash'];
  let tinasFollowers = ['grey', 'mary', 'rex'];
  let mutualFollowers = [];
    

  bobsFollowers.forEach((index, name) => {
    tinasFollowers.forEach((test_index, test_name) => {
      if (tinasFollowers[test_name] == bobsFollowers[name]) {
        mutualFollowers.push(tinasFollowers[test_name])
      }
    })
  })

  console.log(mutualFollowers)
Nico Nisaw
  • 57
  • 1
  • 6