Home > OS >  How can I replace and removing space in the keys in object javascript?
How can I replace and removing space in the keys in object javascript?

Time:04-17

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related