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]);
        }
      }
    }
3
  • 1
    let mutualFollowers = bobsFollowers.filter(f => tinasFollowers.includes(f)); should do it. Commented Jun 15, 2021 at 8:05
  • You are pushing into the wrong array. Push in mutualFollowers mutualFollowers.push(tinasFollowers[k]) . Commented Jun 15, 2021 at 8:06
  • Does this answer your question? Simplest code for array intersection in javascript Commented Jun 15, 2021 at 8:10

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

6 Comments

I am getting output as 'Both followers have undefined' twice
it looks like you add undefined to the array. maybe the index is wrong.
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.
the prototype delivers the methos and the set is thisArg of the filter method. please ahave a look here: Array#filter
yes. if filter does not have thisArg, you could bind it and take the returned function, like Set.prototype.has.bind(new Set(tinasFollowers)).
|
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

Comments

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)

Comments

0

This is giving me this output: [ 'grey' ] [ 'grey', 'mary' ]

I assume that is the desired output.

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(tinasFollowers[k]);
            console.log(mutualFollowers);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.