Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

446
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!