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])
conllection: [object Object]
conllection: [object Object]
conllection: [object Object]

but this is right:
{ 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];
  for (let key in source) {
    if (current[key] !== source[key]) {
      shouldPush = false;
  if (shouldPush) {

// 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/ 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!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.