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

184
Vistas
Cómo filtrar una matriz basada en un valor de campo en cada objeto y agrupar los campos restantes debajo de ese objeto

Estoy tratando de modificar una matriz. Tengo el siguiente json.

 [ { state: "NY", key: "A", value: "Alarm" }, { state: "CT", key: "A", value: "Alarm" }, { state: "NH", key: "A", value: "Alarm" }, { state: "CT", key: "B", value: "Audible and Etched" }, { state: "NH", key: "B", value: "Audible and Etched" }, { state: "NY", key: "AA", value: "Active Disabling and Anti-Theft" }, { state: "FL", key: "B", value: "Audible and Etched" }, { state: "FL", key: "A", value: "Alarm" }, { state: "FL", key: "AA", value: "Active Disabling and Anti-Theft" } ]

Según cada estado único, necesito agrupar la clave, los valores en una matriz desplegable como se muestra a continuación

 [{ state: "NY", dropdown: [ { key: "A", value: "Alarm" }, { key: "AA", value: "Active Disabling and Anti-Theft" } ] }, { state: "CT", dropdown: [ { key: "A", value: "Alarm" }, { key: "B", value: "Audible and Etched" } ] }, { state: "NH", dropdown: [ { key: "A", value: "Alarm" }, { key: "B", value: "Audible and Etched" } ] }, { state: "FL", dropdown: [ { key: "A", value: "Alarm" }, { key: "B", value: "Audible and Etched" }, { key: "AA", value: "Active Disabling and Anti-Theft" } ] }]

Cualquier ayuda sería apreciada. Gracias.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Puede usar Array.reduce para obtener la matriz modificada.

 const inputArray = [ { state: "NY", key: "A", value: "Alarm" }, { state: "CT", key: "A", value: "Alarm" }, { state: "NH", key: "A", value: "Alarm" }, { state: "CT", key: "B", value: "Audible and Etched" }, { state: "NH", key: "B", value: "Audible and Etched" }, { state: "NY", key: "AA", value: "Active Disabling and Anti-Theft" }, { state: "FL", key: "B", value: "Audible and Etched" }, { state: "FL", key: "A", value: "Alarm" }, { state: "FL", key: "AA", value: "Active Disabling and Anti-Theft" } ]; const result = inputArray.reduce((acc, item) => { const exist = acc.find(e => e.state == item.state); if (exist) { exist.dropdown.push({ key: item.key, value: item.value }) } else { acc.push({ state: item.state, dropdown: [{ key: item.key, value: item.value }] }); } return acc; }, []); console.log(result);

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