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

109
Visualizações
How can I make an array instaed of nested arrays in Javascript from myArray?

I need to split riskTypeNo from each object and then assign selectedValue to each value and then create an array with all the objects. With my code I am getting nested arrays

    const myArray = [
  {
    "fieldID": 1681977,
    "riskTypeNo": "A1;M1",
    "selectedValue": "4"
  },
  {
    "fieldID": 1681984,
    "riskTypeNo": "C6;M1",
    "selectedValue": "1"
  },
  {
    "fieldID": 1682084,
    "riskTypeNo": "A13;C6;M1;D5",
    "selectedValue": "3"
  }
];


console.log(
    myArray.map(item1 => {
    const riskTypeNo =  item1.riskTypeNo.split(";").map(item2 => ({ "riskTypeNo": item2,  "selectedValue": item1.selectedValue }));
    return riskTypeNo;   
  })
);

Desired Result is:

[ 
{ riskTypeNo: "A1", selectedValue: "4" }, { riskTypeNo: "M1", selectedValue: "4" }], [{ riskTypeNo: "C6", selectedValue: "1" }, { riskTypeNo: "M1", selectedValue: "1" }], [{ riskTypeNo: "A13", selectedValue: "3" }, { riskTypeNo: "C6", selectedValue: "3" }, { riskTypeNo: "M1", selectedValue: "3" }, { riskTypeNo: "D5", selectedValue: "3" } 
}]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can return the split array from inside the map method. But that will get you an array of arrays. To flatten those arrays just use the array method flat.

This should work:

const result = myArray.map(el => {
    const riskTypes = el.riskTypeNo.split(";").map(riskTypeNo => ({
        riskTypeNo,
        selectedValue: el.selectedValue,
    }))
    return riskTypes;                                           
}).flat();
about 4 years ago · Juan Pablo Isaza Relatório

0

Here's one way:

const myArray = [
  {
    "fieldID": 1681977,
    "riskTypeNo": "A1;M1",
    "selectedValue": "4"
  },
  {
    "fieldID": 1681984,
    "riskTypeNo": "C6;M1",
    "selectedValue": "1"
  },
  {
    "fieldID": 1682084,
    "riskTypeNo": "A13;C6;M1;D5",
    "selectedValue": "3"
  }
];

const result = myArray.reduce((prev,cur) => {
    let obj = [];
    cur.riskTypeNo.split(';').forEach((n) => {
        obj.push({riskTypeNo: n, selectedValue: cur.selectedValue});
    });
    prev.push(...obj);
    return prev;
}, []);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use this method instead :

const newArray = [];
myArray.forEach((item1) => {
  item1.riskTypeNo.split(";").forEach((item2) => {
    newArray.push({ riskTypeNo: item2, selectedValue: item1.selectedValue });
  });
});

result will be in newArray.

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