Why can't I used .concat in 'Wherefore art thou' challenge?

Using .push() causes an array within an array [0,[0,1]]
Using .concat() should fix my problem however something new came up! None of the objects are concatenated. Any reason why?


function whatIsInAName(collection, source) {
 
  var arr = [];
  // Only change code below this line
  var tempArr = []; //Used for console.log() to check if objects are returned here and it is! See within for loop
  var sourceKeys = Object.keys(source); //Array of keys in Source

  for(let i=0; i < sourceKeys.length; i++){ //Iterate thru sourceKeys
    tempArr = collection.filter((obj)=>{ //Each obj in collection Arr filtered by sourceKeys
      if(source[sourceKeys[i]] === obj[sourceKeys[i]]){
        return true;
      }
      else{
        return false;
      }

    });
    arr.concat(tempArr); /*Doesn't work. I've tried directly 'arr.concat(collection.filter(callback))' and same outcome . An empty array for arr*/
  }
  // Only change code above this line
  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/

concat returns a new array; the other arrays are unchanged.

Array.prototype.concat()

Also, please blur your code snippets when the code is complete or almost complete so as to avoid spoiling it for others right away when they see the page.

Thanks for the comment. I understand what you are saying.

I am not trying to change the other arrays, only want to return a new array to a new variable. However if I filter an array of objects why can I not then after ‘add it on’ to an array using .concat() ?

tempArr holds each object in each iteration. But the .concat(tempArr) only results in an empty array into arr. Using console.log(tempArr) shows there is in fact objects in it. I don’t understand how arr can be empty then.

Am I missing some info on how I am using .concat()? I can solve the problem a different way but I just can’t believe .concat() would not work in this situation.

concat will work, but not the way that you think that it will.

If

let arr = [];
let tempArr = [1,2];
let newArr = arr.concat(tempArr);

newarr will be [1,2], arr will be , and tempArr will be [1,2]. If you want to update arr each time instead of using a new variable:

arr = arr.concat(tempArr);

Then arr will be [1,2].

console illustration:

2018-08-30_15-41-33

If on the next go-around, then

tempArr = [3,4]
arr = arr.concat(tempArr);

will have arr = [1,2,3,4]

1 Like

If I read a little more rather than asking you, I would of got that. I feel a little dumber every time I see such a small mistake I made. Anyways thanks for your time haha

You’re welcome. :smile: