Change object key using Object.keys ES6

ecmascript-6, javascript, reactjs

Solution

Here's how I solved it. I used a map to map between existing key and new key. Just substitute the map with whatever new values you need. Finally remove old keys from the object using `omit`.

var tab = {
  abc:1,
  def:40,
  xyz: 50
}

var map = {
    abc : "newabc",
    def : "newdef",
    xyz : "newxyz"
}


_.each(tab, function(value, key) {
    key = map[key] || key;
    tab[key] = value;
});


console.log(_.omit(tab, Object.keys(map)));

Problem

I have ``` var tab = { abc:1, def:40, xyz: 50 } ``` I want to change the name of abc,def, xyz to something else, is it possible? I tried ``` const test = Object.keys(tab).map(key => { if (key === 'abc') { return [ a_b_c: tab[key] ] } }); console.log(test); ``` I got many undefined keys.

Original source

Related problems