Hi there, I’ve come across some very odd behaviour when using concat within a function. The easiest way for me to explain is with some code:
function matchStuff(objArr, stuff) {
var arr = [];
// What's going on here?
console.log(arr.concat(objArr[0]));
// outputs a populated array
//[ { first: 'Romeo', last: 'Montague' } ]
arr.concat(objArr[0]);
console.log(arr);
// outputs [] (an empty array)
// **********************
return arr;
}
matchStuff([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
This seems completely nuts to me! Could someone please shed light on this because I quite often test stuff by putting it in a console.log()
statement first to make sure it does what I expect. I got tripped up a bit here though! Do I need to change this way of testing stuff?
Thanks in advance for any help on this.
LT