Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
How does Array.prototype.reduce() update initialValue via previousValue?

I found a way to build objects dynamically from text paths. The solution relies on the reducer passed to Array.prototype.reduce() updating initialValue parameter via previousValue callback parameter. Here is a simplified example -

const initialValue = {};
['a', 'b', 'c'].reduce((previousValue, currentValue) => {
  previousValue[currentValue] = {};
  return previousValue[currentValue];
}, initialValue);
console.log(initialValue); //{ a: { b: { c: {} } } }

Apparently the inner most {} of initialValue is replaced with { currentValue: {}} on each iteration. Where is this behaviour defined? In case it is not defined, how else could an object be built based on text paths?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!