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

170
Visualizações
Split objects in array, based on the property

I want to duplicate entire objects inside an array, based on the properties inside the object. I need to duplicate the objects, based on the split emails, in nominations.

For example

array = [
{
  id:1,
  name: ravi,
  nominations: xyz@gmail.com, abc@gmail.com
},
{
   id:2
   name: ramu,
   nominations: 123@gmail.com, 456@gmail.com
}
]

Need Output like

Output_array = [
{
  id:1,
  name: ravi,
  nominations: xyz@gmail.com
},
{
  id:1,
  name: ravi,
  nominations: abc@gmail.com
},
{
   id:2
   name: ramu,
   nominations: 123@gmail.com
},
{
   id:2
   name: ramu,
   nominations: 456@gmail.com
}
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The easiest way would be to flatMap over the items, then map over the nominations.

const data = [{
  id:1,
  name: "ravi",
  nominations: "xyz@gmail.com, abc@gmail.com"
},
{
   id:2,
   name: "ramu",
   nominations: "123@gmail.com, 456@gmail.com"
}];

const result = data.flatMap(item => {
  return item.nominations.split(", ").map(email => ({
    id: item.id,
    name: item.name,
    nomination: email
  }))
})

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

A pretty straightforward way using loops:

let data = [
  {
    "id": 1,
    "name": "ravi",
    "nominations": "xyz@gmail.com,abc@gmail.com"
  },
  {
    "id": 2,
    "name": "ramu",
    "nominations": "123@gmail.com,456@gmail.com"
  }
];

let newData = []
for (let element of data) {
  let emailIds = element.nominations.split(",");
  if (emailIds.length > 1) {
    for (let emailId of emailIds)
      newData.push({ id: element.id, name: element.name, nominations: emailId })
  }
}

console.log(newData)

Explanation: Starting with traversing the entire objects, in each object you split the nominations string and split it with "," to check if there is more than one email. If it does exist, you run a loop to individually add them.

about 4 years ago · Juan Pablo Isaza Relatório

0

This could be done with an arguably long one-liner. Object.assign function can be employed to duplicate input object while changing property/properties of interest.

let input = [{
    id: 1,
    name: `ravi`,
    nominations: `xyz@gmail.com, abc@gmail.com`
},
{
    id: 2,
    name: `ramu`,
    nominations: `123@gmail.com, 456@gmail.com`
}];

let output = input.flatMap(i => i.nominations.split(",").map(n => Object.assign({}, i, { nominations: n.trim() })));

console.log(output);

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