Engine bug on Array.reduce
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
People
(Reporter: ktmd, Unassigned)
Details
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36
Steps to reproduce:
Please take a look at https://jsfiddle.net/vjq0786f/ and https://jsfiddle.net/cytpk0ah/
Actual results:
When JSON.stringify(obj1) on https://jsfiddle.net/vjq0786f/. It returns
{"A1":{"boxId":"A1","isSquare":true,"Q6":{"boxId":"Q6","isSquare":false,"C5":{"boxId":"C5","isSquare":true}}},"Q6":{"boxId":"Q6","isSquare":false,"C5":{"boxId":"C5","isSquare":true}},"C5":{"boxId":"C5","isSquare":true}}
Meanwhile, JSON.stringify(obj1) on https://jsfiddle.net/cytpk0ah/ returns
{"A1":{"boxId":"A1","isSquare":true},"Q6":{"boxId":"Q6","isSquare":false},"C5":{"boxId":"C5","isSquare":true}}
The above 2 jsfiddle are pretty much identical, except /vjq0786f have another independent function (no side effect).
Expected results:
Produces the same result
Comment 1•5 years ago
|
||
Can you provide the following information?
- which OS do you use to observe the issue?
- which browser and version do you use to observe the issue?
- if it's Firefox, does the issue happen on Safe mode[1] ?
[1] https://support.mozilla.org/en-US/kb/troubleshoot-firefox-issues-using-safe-mode
Comment 2•5 years ago
|
||
Thanks for the bug report. I think this is a problem with your code (I see the same behavior in Safari).
The above 2 jsfiddle are pretty much identical, except /vjq0786f have another independent function (no side effect).
var obj2 = boxes.reduce((accumulator, currentValue) => {
return accumulator[currentValue.boxId] = currentValue;
}, {});
This does have side-effects: the callback's return value (currentValue in this case) is used as accumulator so it is modified later on. It should be more like what you did with obj1:
var obj2 = boxes.reduce((accumulator, currentValue) => {
accumulator[currentValue.boxId] = currentValue;
return accumulator;
}, {});
Updated•5 years ago
|
Description
•