Published on

Iterating an objects using Object.keys

Authors

I've encountered this problem while doing a skype interview with live coding shared screen. So I decided to make some blog post for it? Anyway the problem was that I need to convert a object like this:

{
  a: "when people run in circles it\'s a very very mad world, mad world",
  b: "every wants to rule the world",
  c: "shout shout let it all out",
  d: "something happens and I'm head over heels"
};

to match this one.

{
  a: 'WHEN PEOPLE RUN IN CIRCLES IT\'S A VERY VERY MAD WORLD, MAD WORLD',
  b: 'EVERY WANTS TO RULE THE WORLD',
  c: 'SHOUT SHOUT LET IT ALL OUT',
  d: 'SOMETHING HAPPENS AND I\'M HEAD OVER HEELS'
}

The quick solution for this one is:

const formatObject = (data) => {
  const newData = {};
  Object.entries(data).map(object => {
    const key = object[0];
    const value = object[object.length - 1];
    newData[key] = value.toUpperCase();
  });
  return newData;
}

Thank you!