I'm trying to write a reduce statement that given an array of strings, return the array index that contains the word 'lace'.
I got it to work with a multi-line if statement, but it doesn't work when I use a single-line if statement:
input array
arr = [ 'tasselled', 'black', 'low-top', 'lace-up' ]
expected output
[3] // since the string 'lace' is in the 3rd index of the array
My code
// works (multi-line if statement)
arr.reduce( function(a,e,i) {
if (e.indexOf('lace') >= 0) {
a.push(i)
}
return a
}, [])
// returns [3]
// doesn't work (single-line if statement)
arr.reduce( (a,e,i) => e.indexOf('lace')>=0 ? a.push(i) : 0, []);
// side note - can you do single-line if-statements without the else statement? (without the ': 0')
// returns error:
TypeError: a.push is not a function