Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda