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

268
Views
JavaScript - adding extra keys values to an object using spread operation or alternative approaches

Here is the code:

    let bucket = [[],[],[],[],[],[],[],[],[],[]];

    bucket = {...Object.keys(bucket)
                       .sort((a,b) => b-a)
                       .filter(key => key > 0)
                       .map(key => '-'+key), 
              ...bucket};

   console.log(bucket);

Problem: the first line of code is not adding the negative keys of the original bucket object into the object, with all properties (keys) having empty arrays as their corresponding value.

Bucket only shows its original properties and values after this line of code is evaluated

How can I get this to work?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The Array.map() creates an array of strings with your negative keys, and they are overwritten when you spread the bucket array. Instead create pairs of [new key, []] convert to an object with Object.fromEntries(), and then spread them:

const bucket = [[],[],[],[],[],[],[],[],[],[]];

const result = {
  ...Object.fromEntries(
    Object.keys(bucket)
      .filter(key => key > 0)
      .sort((a, b) => b-a)
      .map(key => [-key, []])
  ),
  ...bucket};

console.log(result);

about 4 years ago · Juan Pablo Isaza 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!