3

I have object like this :

let output = 
[
  {
    Email: '[email protected]',
    KolomB: 'Haha',
    KolomC: 6,
    ' Kolom D ': '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  },
  {
    Email: '[email protected]',
    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: '[email protected]',
    KolomB: 'Haha',
    KolomC: 6,
    KolomD: '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  },
  {
    Email: '[email protected]',
    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

5
  • You can't modify keys. You can either create a new object or add a new property and remove the old one. Commented Apr 17, 2022 at 12:35
  • yea I would suggest adding a new property and removing the old one using delete Commented Apr 17, 2022 at 12:37
  • I was thinking making a new object with new keys. But, how can I insert the value from output to my new object with new keys ? Commented Apr 17, 2022 at 12:40
  • Why do you need two replace? I don't understand why the second replace is necessary. Commented Apr 17, 2022 at 13:02
  • yes, you right. that was my mistake using second replace. Commented Apr 17, 2022 at 13:47

3 Answers 3

2

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: '[email protected]', KolomB: 'Haha', KolomC: 6, ' Kolom D ': '3.000.000', KolomE: '2022-01-01T16:59:48.000Z' }, { Email: '[email protected]', 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

const output = [ { Email: '[email protected]', KolomB: 'Haha', KolomC: 6, ' Kolom D ': '3.000.000', KolomE: '2022-01-01T16:59:48.000Z' }, { Email: '[email protected]', 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 );

Comments

0

You can try this:

let output = 
[
  {
    Email: '[email protected]',
    KolomB: 'Haha',
    KolomC: 6,
    ' Kolom D ': '3.000.000',
    KolomE: 2022-01-01T16:59:48.000Z
  },
  {
    Email: '[email protected]',
    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;
 })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.