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

220
Views
Best way to create an array from an object that has number of copies

From an object like this:

{a:1, b: 2, c: 3}

I would like to turn into

['a', 'b', 'b', 'c', 'c', 'c']

Where the key is the string and the value is the number of copies, order doesn't matter.

What's the best way to do this?

I was thinking about using array.fill but not sure if that's actually easier than just iterating and push.

Edit: Currently this:

const arr = []
_.each(obj, function (v, k) {
  _.times(v, function () {
    arr.push(k)
  })
})
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You could flatMap the Object.entries and fill an array of each size.

const obj = { a: 1, b: 2, c: 3 };
const result = Object.entries(obj).flatMap(([k, v]) => Array(v).fill(k));

console.log(result)

or with Lodash

const obj = { a: 1, b: 2, c: 3 };
const arr = _.flatMap(obj, (v,k) => Array(v).fill(k))

console.log(arr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

But there's nothing like a simple loop

const obj = { a: 1, b: 2, c: 3 };
const result = []

for (let [k, v] of Object.entries(obj)) {
  while (v--) {
    result.push(k)
  }
}

console.log(result)

about 4 years ago · Santiago Gelvez Report

0

You can use Object.entries and Array#reduce as follows:

const input = {a:1, b: 2, c: 3};

const output = Object.entries(input).reduce(
    (prev, [key,value]) => prev.concat( Array(value).fill(key) ),
    []
);

console.log( output );

Or, using Array#push instead of Array#concat,

const input = {a:1, b: 2, c: 3};

const output = Object.entries(input).reduce(
    (prev, [key,value]) => prev.push( ...Array(value).fill(key) ) && prev,
    []
);

console.log( output );

Or, using for loops,

const input = {a:1, b: 2, c: 3};

const output = [],
      pairs = Object.entries(input);
for(let i = 0; i < pairs.length; i++) {
    const [key, value] = pairs[i];
    for(let j = 0; j < value; j++) {
        output.push( key );
    }
}

console.log( output );

about 4 years ago · Santiago Gelvez Report

0

I would convert the object into an array of keys using Object.keys and then use a newly created empty results array, then map through the keys.

For each key I would add a fill array to the existing results.

Here's the ES6 solution to that (no extra libraries required)

const obj = { a: 1, b: 2, c: 3 };
let result = []
Object.keys(obj).forEach(key => {
  result = [...result, ...new Array(obj[key]).fill(key)]
})

console.log(result)

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!