Notice the difference between the way names and names2 is handled. Why am I getting this difference? How can I make names behave like names2?
BTW, I get the same behavior in Firefox.
> names
[
'Liam', 'Noah',
'Oliver', 'Elijah',
'William', 'James',
'Benjamin', 'Lucas',
'Olivia', 'Emma',
'Ava', 'Charlotte',
'Sophia', 'Amelia',
'Isabella', 'Mia'
]
> names2
[ 'Noah', 'Oliver', 'Sophia', 'Mia' ]
> names .map((n)=>n+'\n') .reduce((x,y)=>x+y,'')
'Liam\n' +
'Noah\n' +
'Oliver\n' +
'Elijah\n' +
'William\n' +
'James\n' +
'Benjamin\n' +
'Lucas\n' +
'Olivia\n' +
'Emma\n' +
'Ava\n' +
'Charlotte\n' +
'Sophia\n' +
'Amelia\n' +
'Isabella\n' +
'Mia\n'
> names2 .map((n)=>n+'\n') .reduce((x,y)=>x+y,'')
'Noah\nOliver\nSophia\nMia\n'
>
This is just how long strings are displayed.
For example, try typing this in a node console:
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
it will show this output:
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n' +
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
The first part of my question was the important part. The answer I now think is that this behavior is a optimization in some Javascript engines to provide lazy evaluation of reduce for large arrays.
The second question can now be rephrased as "how to force that lazy evaluation to run to completion the way console.log does?" An answer to that would be interesting but I don't need it as I am capable of writing my own reduce function.