0

Could someone please explain the syntax of this code. I dont need an explanation of 'some' function just the

(arrayValue => value === arrayValue);

within this function

function checkAvailability(array, value) {
  return array.some(arrayValue => value === arrayValue);
}
pilchard
  • 12,414
  • 5
  • 11
  • 23
  • It looks like you need an explanation of [`some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some). `arrayValue => value === arrayValue` is the callback passed to `some()` comparing each iterated `arrayValue` to the passed `value` parameter. see: [Some: Testing array elements using arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#testing_array_elements_using_arrow_functions) – pilchard Jan 03 '22 at 13:19
  • To break down the callback syntax, `arrayValue => value === arrayValue` is an arrow function, where `arrayValue` is an argument, and the result of evaluating the comparison `value === arrayValue` is the return value. – Nick Parsons Jan 03 '22 at 13:20
  • 1
    `x => y` is called an [Arrow function expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – Peter B Jan 03 '22 at 13:23

3 Answers3

0

arrayValue refers to the abstract name of the item which is currently passing into your loop. You are checking if the function parameter value is equal to it. The final goal is here to check if there is at least one arrayValue that equals to value. The function will return a boolean.

For exemple:

const arr = ["joe", "jack"]    

function checkAvailability(array, value) { 
  return array.some(arrayValue => value === arrayValue);
 }

//  is there at least one item "joe" in the array. True.
console.log(checkAvailability(arr, "joe"))

//  is there at least one item "lola" in the array. False.
console.log(checkAvailability(arr, "lola"))
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
0

That's just a condition that checks if the given value is in the array.

Meaning, this logic takes each value of the array and checks the condition "value === arrayValue". where value is the given value and arrayValue is an element of the array.

And by definition of array.some, if this condition is true for at least one element of the array.

Then "array.some(arrayValue => value === arrayValue)" returns true else return false

0

That part of the function is an arrow function as It has only on single line you don't need to have curly brace and return key.

The code is equal to this one

function checkAvailability(array, value) {
    return array.some(function(arrayValue) {
        return value === arrayValue;
    })
}

As there is only one line in the body of callback function you can directly return it within a arrow function like this

array.some(arrayValue => value ===arrayValue)

And that part is the function which is use to check if the array have already some item which match the filter condition difine in the callback in this case value === arrayValue

Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31