Here's the code in question:
Object.entries({
orderStatus: null,
orderType: null,
limit: 25,
offset: null,
sortFields: null
}).reduce((acc, [a, b]) => b ? acc[a] = b : acc, {});
A console.log shows that the first time acc[a]
is set, it just replaces acc
with the string in a
, then tries assign a property to acc
(which is now a string) and throws the TypeError. When I use:
.reduce((acc, [a, b]) => b ? {...acc, ...{[a]: b}} : acc, {});
it works like a dream. Why would acc[a]
be setting acc
to 25 once it hits limit
? shouldn't that be the same as acc["limit"] = 25
?