Я пытаюсь написать оператор redu * , который дает массив строк, возвращает индекс массива, содержащий слово 'lace'.
Я получил его для работы смногострочный оператор if, но он не работает, если я использую однострочный оператор if:
массив ввода
arr = [ 'tasselled', 'black', 'low-top', 'lace-up' ]
ожидаемый вывод
[3] // since the string 'lace' is in the 3rd index of the array
Мой код
// 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