-1

Why is this simple example returning an empty array?

const arr = [1, 2];
const result = arr.reduce((list, val) => {
  list.push[val];
  return list;
}, []);

console.log(result);
LongHike
  • 4,016
  • 4
  • 37
  • 76

1 Answers1

0

You are using Array.prototype.push in the wrong way.

const arr = [1, 2];
const result = arr.reduce((list, val) => {
  list.push(val);
  return list;
}, []);

console.log(result);
Akshay Bande
  • 2,491
  • 2
  • 12
  • 29