Console.log("conllection: " + collection[i]) error

Tell us what’s happening:
Thank you for help me out :heart:
why I can’t:
console.log("conllection: " + collection[i])
console:
conllection: [object Object]
conllection: [object Object]
conllection: [object Object]

but this is right:
console.log(collection[i])
console:
{ first: ‘Romeo’, last: ‘Montague’ }
{ first: ‘Mercutio’, last: null }
{ first: ‘Tybalt’, last: ‘Capulet’ }

  **Your code so far**
function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
for (let i = 0; i < collection.length; i++){
  let shouldPush = true;
  let current = collection[i];
  console.log(collection[i])
  for (let key in source) {
    if (current[key] !== source[key]) {
      shouldPush = false;
    }
  }
  if (shouldPush) {
    arr.push(current);
  }
}

// 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Wherefore art thou

Link to the challenge:

Its because you are turning an object into a string with the +, and that is what it returns. why? i don’t know. But if you try console.log("collection:", collection[i]) it will stay an object and display properly.

1 Like

Oh, that’s works! But I am still curious why + can’t work :upside_down_face:

and the ${collection[i]} doesn’t work too!

The default serialisation of an object, to a string in JS will result in [Object object].
Or in other words that’s how JS represents object as strings.

It gets converted into a string because of the + concatenation, so the JS runtime will attempt to serialise the object in a string and “attach” it to the previous string.

The template string will suffer the same fate, JS will serialise the object into a string, and that’s the default .

If you want to properly serialise an obj into a string you should use JSON.stringify.

Hope this helps :sparkles:

1 Like

Thanks! You are so pro!!