You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to do something that I thought would be easy, but running into some roadblocks...
As an example, let's try to convert an input Month abbreviation into the Date month number (0-based). It's easy to find out if the incoming string is in the array of months, using payload in $months, but finding out what position it occupies in the array is not so easy:
A bit clunky, perhaps, but it returns the index number for every array element that is equal to the incoming string. I tried using the $reduce($array, function($total, $value)) -- but unlike the Javascript function of the same name, there is no index value passed as the 3rd argument. At a minimum, I think the $reduce() function should be extended to also pass $index as an optional argument.
But perhaps a pair of new functions would be more intuitive and simpler to use, such as:
$find(array, input) and the overloaded $find(array, function($value))
$indexOf(array, input) and the overloaded $indexOf(array, function($value))
Both functions take an array of "things", and either a similar "thing" to find, or a function that determines which element is a match (returns true or false).
The text was updated successfully, but these errors were encountered:
At a minimum, I think the $reduce() function should be extended to also pass $index as an optional argument.
+1
In addition to the optional 3rd argument of array index, the signature for the callback function should include an optional 4th argument which is the array being iterated over. This would be consistent with the ECMAScript behavior of reduce. Passing the array index and the parent array would also be more consistent with the signature of the callback argument to $map.
I'm trying to do something that I thought would be easy, but running into some roadblocks...
As an example, let's try to convert an input Month abbreviation into the Date month number (0-based). It's easy to find out if the incoming string is in the array of months, using
payload in $months
, but finding out what position it occupies in the array is not so easy:A bit clunky, perhaps, but it returns the index number for every array element that is equal to the incoming string. I tried using the
$reduce($array, function($total, $value))
-- but unlike the Javascript function of the same name, there is no index value passed as the 3rd argument. At a minimum, I think the$reduce()
function should be extended to also pass $index as an optional argument.But perhaps a pair of new functions would be more intuitive and simpler to use, such as:
$find(array, input)
and the overloaded$find(array, function($value))
$indexOf(array, input)
and the overloaded$indexOf(array, function($value))
Both functions take an array of "things", and either a similar "thing" to find, or a function that determines which element is a match (returns true or false).
The text was updated successfully, but these errors were encountered: