• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

372
Vistas
How can I replace and removing space in the keys in object javascript?

I have object like this :

let output = 
[
  {
    Email: 'haha@yopmail.com',
    KolomB: 'Haha',
    KolomC: 6,
    ' Kolom D ': '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  },
  {
    Email: 'blabla@gmail.com',
    KolomB: 'blabla',
    KolomC: 6,
    ' Kolom D ': '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  }
]

I want to remove the spaces if occurs in keys inside the object.

this is my desired output :

output = 
[
  {
    Email: 'haha@yopmail.com',
    KolomB: 'Haha',
    KolomC: 6,
    KolomD: '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  },
  {
    Email: 'blabla@gmail.com',
    KolomB: 'blabla',
    KolomC: 6,
    KolomD: '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  }
]

As you can see, i want to remove the space in Kolom D to be KolomD in output variable.

Here's what i've done before :

for (let i = 0; i < output.length; i++) {
 Object.keys(output[i]).forEach(function(key) {
  key = key.replace(/\s+/g, "").replace(/^\s+|\s+$/g, "")
 })
}

but the output still not changing. How to push/changing the keys to the output ?

Please let me know if you need more information if it's still not enough to solve that problem

almost 3 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can't modify keys. You can either create a new object or add a new property and remove the old one.

This is a way to create a new object

let output = [{ Email: 'haha@yopmail.com', KolomB: 'Haha', KolomC: 6, ' Kolom D ': '3.000.000', KolomE: '2022-01-01T16:59:48.000Z' }, { Email: 'blabla@gmail.com', KolomB: 'blabla', KolomC: 6, ' Kolom D ': '3.000.000', KolomE: '2022-01-01T16:59:48.000Z' }];

output = output.map(el => 
  Object.fromEntries(Object.entries(el).map(([key, value]) => ([
    key.replace(/\s+/g, ""),
    value
  ])))
);

console.log(output);

The first map iterates over the array and converts each element. Object.entries converts each element to an array. The second map applies the algorithm from your question to each key. Object.fromEntries converts the array back to an object.

I also removed the second .replace. It's not necessary. The first replace already removes all spaces.

almost 3 years ago · Juan Pablo Isaza Denunciar

0

You can try this:

let output = 
[
  {
    Email: 'haha@yopmail.com',
    KolomB: 'Haha',
    KolomC: 6,
    ' Kolom D ': '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  },
  {
    Email: 'blabla@gmail.com',
    KolomB: 'blabla',
    KolomC: 6,
    ' Kolom D ': '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  }
]
for (let i = 0; i < output.length; i++) {
 Object.keys(output[i]).forEach(function(key) {
  const newkey = key.replace(/\s+/g, "").replace(/^\s+|\s+$/g, "")
  const value = output[i][key];
  delete output[i][key];
  output[i][newkey] = value;
 })
}
almost 3 years ago · Juan Pablo Isaza Denunciar

0

You can use a for...of loop and String#replace:

const output = [ { Email: 'haha@yopmail.com', KolomB: 'Haha', KolomC: 6, ' Kolom D ': '3.000.000', KolomE: '2022-01-01T16:59:48.000Z' }, { Email: 'blabla@gmail.com', KolomB: 'blabla', KolomC: 6, ' Kolom D ': '3.000.000', KolomE: '2022-01-01T16:59:48.000Z' } ];

for(let [i, obj] of Object.entries(output)) {
    const newobj = Object.fromEntries(
        Object.entries(obj).map(([key,value]) => [key.replace(/\s+/g, ''),value])
    );
    output[+i] = newobj;
}

console.log( output );

almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda