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

118
Views
Repeat every element in array based on object properties

I have an array that I'm retrieving from an API. The array looks like this:

[{
    "name": "Rachel",
    "count": 4,
    "fon": "46-104104",
    "id": 2
},
{
    "name": "Lindsay",
    "count": 2,
    "fon": "43-053201",
    "id": 3
},
{
    "name": "Michael",
    "count": 5,
    "fon": "46-231223",
    "id": 4
}]

Then I loop through the array to create an array containing only the names.

function buildName(data) {
  for (var i = 0; i < data.length; i++) {
    nameList.push(data[i].name)
  }
}

This also works so far, but I would like to create an array in which each name occurs as often as the object count says.

For example, the name Michael should appear five times in the array and Lindsay twice.

[
  "Rachel",
  "Rachel",
  "Rachel",
  "Rachel",
  "Lindsay",
  "Lindsay",
  "Michael",
  "Michael",
  "Michael",
  "Michael"
  "Michael"
]
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

For each object create a new array using count, and then fill it with the name.

If you use flatMap to iterate over the array of objects. It will return a new array of nested objects but then flatten them into a non-nested structure.

const data=[{name:"Rachel",count:4,fon:"46-104104",id:2},{name:"Lindsay",count:2,fon:"43-053201",id:3},{name:"Michael",count:5,fon:"46-231223",id:4}];

const out = data.flatMap(obj => {
  return new Array(obj.count).fill(obj.name)
});

console.log(out);

about 4 years ago · Santiago Trujillo Report

0

Use an inner while loop inside the for loop:

const data = [{
    "name": "Rachel",
    "count": 4,
    "fon": "46-104104",
    "id": 2
},
{
    "name": "Lindsay",
    "count": 2,
    "fon": "43-053201",
    "id": 3
},
{
    "name": "Michael",
    "count": 5,
    "fon": "46-231223",
    "id": 4
}]


function buildName(data){
  const result = [];
  for (let i = 0; i < data.length; i += 1) {
    let item = data[i];
    let count = item.count;
    while (count > 0) {
      result.push(item.name);
      count -= 1;
    }
  }
  return result;
}


console.log(buildName(data));

about 4 years ago · Santiago Trujillo Report

0

You can use .flapMap() for that:

const arr = [{ "name": "Rachel", "count": 4, "fon": "46-104104", "id": 2 }, { "name": "Lindsay", "count": 2, "fon": "43-053201", "id": 3 }, { "name": "Michael", "count": 5, "fon": "46-231223", "id": 4 }];

const result = arr.flatMap(({count, name}) => Array(count).fill(name));
console.log(result);

Effectively you turn every element into an array of the the name property repeated count times which is then flattened into a single array.

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