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

332
Views
Why can't you assign a dictionary in a map operation in JS like this?

Can someone explain to me my error in logic for why this doesn't work to assign a key value pair in javascript and initialize their values to 0? The values return as undefined.

What is the best way to do this instead?

Example:

const letters = ['i','c','l','e'];
let dict = {};

dict = letters.map(value => dict[value] = 0);
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

map() returns an array of the results of the callback. So you're replacing the object with that array.

Since you're modifying the object in the loop, you don't need to assign back to the variable. And since you don't need an array of the results, you should use forEach() rather than map()

letters.forEach(letter => dict[letter] = 0);

BTW, in JavaScript we call these objects, not dictionaries.

about 4 years ago · Santiago Gelvez Report
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!