+ 2
Or use array method, map
const fruits2 = fruits.map(fruit => fruit ? fruit : "new fruit");
0
reduce()
reduce() loops through original array,
and then returns a "result" (Not necessarily an array).
reduce() accepts two parameters:
1. A reducer function
2. Initial Value
For the reducer function, it accepts two to four parameter:
1. Accumulator
2. Each item of original array
3. (Optional) Index of the item in original array
4. (Optional) Original Array
Demo:
https://code.sololearn.com/cqHxL5WFU1PQ/?ref=app
Ref:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce