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.
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.
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