стрелочные функции vs function
Не могу понять. Почему с обычными функция результат undefined, а со стрелочными результат ожидаемый?
let testArr = [
[1,2,3],
[4,5,6],
[7,8,9],
]
console.log (testArr)
let sumArr = testArr.map(function(item){
item.reduce(function(acc, item){
return acc + item;
})
})
console.log (sumArr)
let sumArr1 = testArr.map(item => item.reduce((acc, item) => acc + item));
console.log (sumArr1)
Ответы (1 шт):
Автор решения: u111
→ Ссылка
Вы забыли написать return
. Правильный код:
let testArr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
console.log(testArr);
let sumArr = testArr.map(function(item){
return item.reduce(function(acc, item){
return acc + item;
})
})
console.log(sumArr);
let sumArr1 = testArr.map(
item => item.reduce(
(acc, item) => acc + item
)
)
console.log(sumArr1);