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

205
Vistas
Filtre objetos en una matriz según la expresión regular aplicada a la propiedad

Tengo esta estructura de datos:

 const data = [ { id: "1", name: "first item", T1_something: 21, T1_anotherthing: 15, T1_hello: 98, T2_coding: 12.3, T2_world: 4.1, T2_different: 2.5 }, { id: "2", name: "second item", T1_something: 3, T1_anotherthing: 12.6, T1_hello: 64.1, T2_coding: 71.3, T2_world: 4.5, T2_different: 3.3 } ];

Quiero obtener solo propiedades donde el nombre de la propiedad coincida con la siguiente expresión regular /^T\d/

Así que mi salida deseada es esta:

 const desiredResult = [ { T1_something: 21, T1_anotherthing: 15, T1_hello: 98, T2_coding: 12.3, T2_world: 4.1, T2_different: 2.5 }, { T1_something: 3, T1_anotherthing: 12.6, T1_hello: 64.1, T2_coding: 71.3, T2_world: 4.5, T2_different: 3.3 } ];

¿Cómo hago para hacer este filtrado basado en una expresión regular en una matriz de objetos?

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

0

Mi solución

 const desiredResult = data.map(o=>{ const newO={}; Object.keys(o).filter(oKey=>/^T\d/.test(oKey)).forEach(oKey=>{ newO[oKey]=o[oKey] }) return newO; })
about 4 years ago · Juan Pablo Isaza Denunciar

0

 const data = [ { id: "1", name: "first item", T1_something: 21, T1_anotherthing: 15, T1_hello: 98, T2_coding: 12.3, T2_world: 4.1, T2_different: 2.5 }, { id: "2", name: "second item", T1_something: 3, T1_anotherthing: 12.6, T1_hello: 64.1, T2_coding: 71.3, T2_world: 4.5, T2_different: 3.3 } ]; const filterBasedOnProp = (elements, pattern) => { const regex = new RegExp(pattern) return elements.map((element) => Object.keys(element).reduce((accumulator, key) => { if (key.match(regex)) accumulator[key] = element[key] return accumulator }, {})) } console.log(filterBasedOnProp(data, "^T"))

about 4 years ago · Juan Pablo Isaza Denunciar

0

// const data = ... // const regexp = ... function filterMyDataProperties(data, regexp) { return data.map(item => { return Object.keys(item).reduce((newItem, key) => { if (regexp.test(key)) { newItem[key] = item[key] } return newItem }, {}) }) } const result = filterMyDataProperties(data, regexp);
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