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

209
Vistas
Convierta claves en una matriz a minúsculas y elimine espacios entre claves si están disponibles (JS)

Necesito convertir las claves en minúsculas y eliminar los espacios entre ellas, si corresponde. Todas las claves son dinámicas y pueden ser cualquier número de claves. Las claves pueden contener caracteres alfanuméricos junto con caracteres especiales. Javascript o Typescript cualquier cosa me serviría. Estoy un poco atascado aquí.

JSON actual:

 [ { "Outdated Label (Key1)": "Outdated Label1", "Outdated Value": "Outdated Value" }, { "Outdated Label (Key1)": "Outdated Label2", "Outdated Value": "Outdated Value2" }, { "Outdated Label (Key1)": "Outdated Label3", "Outdated Value": "Outdated Value3" }, { "Outdated Label (Key1)": "Outdated Label4", "Outdated Value": "Outdated Value4" } ]

JSON esperado:

 [ { "outdatedlabel(key1)": "Outdated Label1", "outdatedvalue": "Outdated Value" }, { "outdatedlabel(key1)": "Outdated Label2", "outdatedvalue": "Outdated Value2" }, { "outdatedlabel(key1)": "Outdated Label3", "outdatedvalue": "Outdated Value3" }, { "outdatedlabel(key1)": "Outdated Label4", "outdatedvalue": "Outdated Value4" } ]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Eché un vistazo al problema y encontré una solución que funciona y está escrita en JavaScript simple. Esta solución funciona para la matriz dada y cualquier otra matriz de objetos que no estén anidados. Como desea tener una nueva matriz, podemos usar .map y luego iterar sobre cada clave en el objeto actual y modificar la clave de tal manera que satisfaga sus requisitos.

 let o = [ { "Outdated Label (Key1)": "Outdated Label1", "Outdated Value": "Outdated Value" }, { "Outdated Label (Key1)": "Outdated Label2", "Outdated Value": "Outdated Value2" }, { "Outdated Label (Key1)": "Outdated Label3", "Outdated Value": "Outdated Value3" }, { "Outdated Label (Key1)": "Outdated Label4", "Outdated Value": "Outdated Value4" } ] let whitespace = new RegExp(/\s/g); let lo = o.map((entry) => { let modified = {}; Object.keys(entry).forEach((key) => { let value = entry[key]; key = key .toLowerCase() .replace(whitespace, ""); modified[key] = value; }); return modified; }); console.log(lo);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Aquí está mi solución, use la función de map .

 var arr = [ { "Outdated Label (Key1)": "Outdated Label1", "Outdated Value": "Outdated Value" }, { "Outdated Label (Key1)": "Outdated Label2", "Outdated Value": "Outdated Value2" }, { "Outdated Label (Key1)": "Outdated Label3", "Outdated Value": "Outdated Value3" }, { "Outdated Label (Key1)": "Outdated Label4", "Outdated Value": "Outdated Value4" } ]; const expectedJson = arr.map(function(item) { for(const key of Object.keys(item)) { const lowerCaseKey = key.toLowerCase().replace(/\s/g, ''); if (lowerCaseKey !== key) { item[lowerCaseKey] = item[key]; delete item[key]; } } return item; }) console.log(expectedJson)
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