Changing an object key with Object.keys ES6

I have

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

I want to change the name abc, def, xyz to something else, is this possible?

I tried

const test = Object.keys(tab).map(key => {
  if (key === 'abc') {
    return [
      a_b_c: tab[key]
    ]
  }
});

console.log(test);

I got a lot of undefined keys.

+10
source share
9 answers

This is how I solved it. I used a map to display between an existing key and a new key. Just replace the card with the new values ​​you need. Finally, remove the 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)));
+1
source

Here is the complete code for replacing keys based on an object that displays the values ​​to replace:

const tab = {abc: 1, def: 40, xyz: 50};
const replacements = {'abc': 'a_b_c', 'def': 'd_e_f'};

let replacedItems = Object.keys(tab).map((key) => {
  const newKey = replacements[key] || key;
  return { [newKey] : tab[key] };
});

, . , :

const newTab = replacedItems.reduce((a, b) => Object.assign({}, a, b));

: {"a_b_c": 1, "d_e_f": 40, "xyz": 50}

+7

lodash mapKeys .

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

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

// Change keys
_.mapKeys(tab, (value, key) => {
  return map[value];
});

// -> { newabc: 1, newdef: 40, newxyz: 50 }
+3

, :

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

const {'abc': 'a_b_c', 'def': 'd_e_f', ...rest} = tab;
tab = {'a_b_c', 'd_e_f', ...rest}
+2

lodash.

import { mapKeys } from 'lodash';    

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

const test = mapKeys(tab, (value, key) => {
    if (key === 'abc') return 'a_b_c';
    return key;
});
+1

.

const rename = (({abc: a_b_c, ...rest}) => ({a_b_c, ...rest}))
console.log(rename({abc: 1, def: 2}))
// { "a_b_c": 1, "def": 2 }
Hide result
+1

.

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

    var key = 'abc'
    console.log(key)
    tab['a_b_c'] = tab[key]
    delete tab[key]
    
 
console.log(tab);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Hide result
0

.

const options = {
  method: "POST", // *GET, POST, PUT, DELETE, etc.
  ...
  body: JSON.stringify({}) // body data type must match "Content-Type" header
}

window.fetch(url,{
...options,
...{ method: "PUT", body: JSON.stringify(plot) }
})
0

,

:

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

:

let newKeyMappings = {
    abc: 'cab',
    def: 'fed',
    xyz: 'zyx'
};

let mapped = Object.keys(tab).map(oldKey=> {
    let newKey = newKeyMappings[oldKey];
    let result ={};
    result[newKey]=tab[oldKey];
    return result;
});

,

let result = mapped.reduce((result, item)=> {
    let key = Object.keys(item)[0];
    result[key] = item[key];
    return result;
}, {});
-1
source

Source: https://habr.com/ru/post/1017070/


All Articles