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
Reduce nested array of objects to array of objects

I have an array like

Const array=[
{
id:1,
name:"abc",
Address:[
   {
    City: "something",
    Country: "first country"
   },
   {
    City: "other city",
    Country: "country"
    }
  ]
},
{
...........
}
];

I have to display this nested array objects as flat key value list. So How to reduce this like below.

Reducedarray = [
  { Id: 1, name: "abc" },
  { City: "something", country: "first country"},
  { City: "other city", country: "country"},
  { Id: 2, name: "bbc" },
  { City: "bbcsomething", country: "fbbct country"},
  { City: "other city", country: "country"}
]

Using reducearray i will map with object keys and display as key value list in html.

Need to display as flat list using jsx like below

Id: 1 Name: abc City: first ity Country: firstcountry City: second city Country: second country Id:2 Name: another name ..... ...... ....

Can anyone help me on this plz.. is it possible with reduce only?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could take a flat map with the destructured rest object and Address array.

const
    array = [{ id: 1, name: "abc", Address: [{ City: "something", Country: "first country" }, { City: "other city", Country: "country" }] }],
    result = array.flatMap(({ Address, ...o }) => [o, ...Address]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Solution with reduce:

const data = [{ id: 1, name: "abc", Address: [{ City: "something", Country: "first country" }, { City: "other city", Country: "country" }] }, { id: 2, name: "dfe", Address: [{ City: "something1", Country: "second country" }, { City: "city", Country: "new country" }] }];

const Reducedarray = data.reduce((acc, { Address, ...rest }) => (
  [...acc, rest, ...Address]
), []);

console.log(Reducedarray );
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

const array= [
{
id:1,
name:"abc",
Address:[
   {
    City: "something",
    Country: "first country"
   },
   {
    City: "other city",
    Country: "country"
    }
  ]
},
];

const array2 = []

for(let el of array) {
  
    if(el.id) array2.push({id: el.id, name: el.name})
    
    if(el.Address) {
        
        for(let element of el.Address) {
          
          array2.push({ city: element.City, country: element.Country})
        }
    }
  
  
}

console.log(array2)
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