0

I am wondering if someone can explain to my why this is allowed more specifically why I don't need to say in notes.filter(notesCheck) what obj needs to be. The function works fine in this manner.

var duplicateNotes = notes.filter(notesCheck)

function notesCheck(obj) {
    if (obj.title === note.title) {
        console.log("duplicate found")
        return true
    }
}

I read https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter but didn't really gleam an explanation from it.

Summary filter() calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a value that coerces to true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Array elements which do not pass the callback test are simply skipped, and are not included in the new array.

callback is invoked with three arguments:

the value of the element the index of the element the Array object being traversed

9
  • notesCheck is the callback function you supply, it will be called with three arguments for each iteration, the current item, current index, and the whole array Commented Dec 18, 2016 at 2:50
  • When you pass a defined function as a parameter, the parameters for the passed in function will be inferred from the definition. Commented Dec 18, 2016 at 2:50
  • Hey Jaromanda thanks I get that but its strange I can just pluck the current item out like that not needing to in some way say this is that I need Commented Dec 18, 2016 at 2:52
  • "When you pass a defined function as a parameter, the parameters for the passed in function will be inferred from the definition" so this is a common rule? Commented Dec 18, 2016 at 2:55
  • @gxminbdd that's not as much strange as its an elegant abstraction away from the rawness of indexed access :). Try implementing any of the filter, map, reduce functions yourself or find other people's implementations to understand the inner workings of these methods. Commented Dec 18, 2016 at 2:55

1 Answer 1

2

Array.filter takes a function as input, and will call it for each element in the array. It works like:

var myFilter(array, test) {
  var result = [];
  array.forEach(function(obj) {
    if (test(obj)) { result.push(obj); }
  }
  return result;
}

// notes.filter(notesCheck) it the same as myFilter(notes, notesCheck)

So you don't need to specify the obj when calling filter, but just the logic to handle them.

Basically, Array.filter(test) means give me all the elements that pass the test, which in English I'm able to say without telling what are those elements, and so am I in javascript.

Sign up to request clarification or add additional context in comments.

1 Comment

I think @nem035 is right: you'll learn quite a bit by implementing filter, map, reduce by yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.