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);
}
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"))
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
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