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

145
Vistas
Extract array from javascript object

I have below javascript object - (named Division) I want to extract only SubDivs from the array

I tried : -

const division = [{
    Name: "DivName1",
    Value: "DivValue1",
    SubDivision: {
      Name: "SubDiv1",
      Value: "SubDiv1"
    }
  },
  {
    Name: "DivName2",
    Value: "DivValue2",
    SubDivision: [{
        Name: "SubDiv2",
        Value: "SubDiv2"
      },
      {
        Name: "SubDiv3",
        Value: "SubDiv3"
      }
    ]
  }
]



var subDivs = division.map(x => x.SubDivision);
console.log(subDivs)

But this is not giving me array in format -

[{
Name:"SubDiv1",
Value:"SubDiv1"
},
{
Name:"SubDiv2",
Value:"SubDiv2"
},
{
Name:"SubDiv3",
Value:"SubDiv3"
}]
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use flatMap for that

const division = [{
    Name: "DivName1",
    Value: "DivValue1",
    SubDivision: [{
      Name: "SubDiv1",
      Value: "SubDiv1"
    }]
  },
  {
    Name: "DivName2",
    Value: "DivValue2",
    SubDivision: [{
        Name: "SubDiv2",
        Value: "SubDiv2"
      },
      {
        Name: "SubDiv3",
        Value: "SubDiv3"
      }
    ]
  }
]

const subdivision = division.flatMap(d => d.SubDivision)

console.log(subdivision)

about 4 years ago · Santiago Trujillo Denunciar

0

Given your example, all you need to do is call flat on the mapped array:

var subDivs= division.map(x=>x.SubDivision).flat();

Working example:

const division = [{
    Name: "DivName1",
    Value: "DivValue1",
    SubDivision: [{
      Name: "SubDiv1",
      Value: "SubDiv1"
    }
  ]},
  {
    Name: "DivName2",
    Value: "DivValue2",
    SubDivision: [{
      Name: "SubDiv2",
      Value: "SubDiv2"
    },
    {
      Name: "SubDiv3",
      Value: "SubDiv3"
    }
  ]
  }
]

var subDivs= division.map(x=>x.SubDivision).flat();

console.log(subDivs)

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