Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

447
Visualizações
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

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

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.

about 4 years ago · Juan Pablo Isaza Relatório

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;
 })
}
about 4 years ago · Juan Pablo Isaza Relatório

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 );

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda