Why 'obj' in this case is object

hi everyone! Sorry, I am very beginner and do not understand often some moments
about function usage. I am solving Intermediate Algorithm Scripting, and in the task
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

Why does in THAT LINE BELOW the ‘obj’ is the objects { first: ‘Romeo’, last: ‘Montague’ } etc
I see this is iteration, but from where does JS get that?
And maybe anyone could suggest proper reading and explanation for this?
Artem.

function whatIsInAName(collection, source) {
  const arr = [];
  // Only change code below this line
const source_arr = Object.keys(source); // array of Source Keys

//const souceKeys = Object.keys(source);

  //THIS LINE BELOW
  return collection.filter(obj => {
    for (let i = 0; i < source_arr.length; i++) {

      console.log(obj) //checking obj here

      if (!obj.hasOwnProperty(source_arr[i]) ||
          obj[source_arr[i]] !== source[source_arr[i]]) {
        return false;
      }
    }
    return true;
  });

  // Only change code above this line
  return arr;
}

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

collection is an array and filter is a method called on that array which returns a new array. The filter method takes a function as the first argument and then uses this function to determine which items in collection should be “filtered” into the new array it returns. So

obj => {...}

is a function definition using the arrow syntax. The function takes one parameter, which is obj in this case, but you can change that to whatever you want, you get to decide what to name that parameter. It makes sense to use obj here since collection is an array of objects.

The filter method will automatically go through each item in collection and pass it into the function you passed into filter. So each object in the collection array gets passed into your function through the obj parameter.

1 Like

thank you, kevinSmith! I will do!

1 Like

obj is just parameter, and with filter method JS uses {…} arrow function body to decide true-false, right? And for many parameters we have to use another syntax like
const obj =() => {}, isn’t?
It is very confuses me every time.

The callback function for the filter method can actually take more than one parameter. The second parameter is the index number of the current array item. So if you wanted to use that then it would look like:

return collection.filter((obj, index) => {...})

Also, if it makes it easier to understand, you don’t have to use arrow functions. You could use the traditional function definition instead:

return collection.filter(function(obj, index) { ... })

But all the cool kids use arrow functions now.

1 Like

thank you! Now I understand! It was in definition for filter method

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