Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda