showing results for - "reverse key and value in object js"
Sara
01 May 2020
1//The concise way by using ES6 syntax like this.
2
3const obj = {A : 1, B : 2, C : 3, D : 4}
4 function objectFlip(obj) {
5     return Object.entries(obj).reduce((acc, [key, value]) => (acc[value] = key, acc), {})
6 }
7console.log('input:  ',obj);
8console.log('output: ',objectFlip(obj));
9
10
11
12static objectFlip(obj) {
13  const ret = {};
14  Object.keys(obj).forEach(key => {
15    ret[obj[key]] = key;
16  });
17  return ret;
18}
19//Or using Array.reduce() & Object.keys()
20
21static objectFlip(obj) {
22  return Object.keys(obj).reduce((ret, key) => {
23    ret[obj[key]] = key;
24    return ret;
25  }, {});
26}
27//Or using Array.reduce() & Object.entries()
28
29static objectFlip(obj) {
30  return Object.entries(obj).reduce((ret, entry) => {
31    const [ key, value ] = entry;
32    ret[ value ] = key;
33    return ret;
34  }, {});
35}
Elisa
17 Oct 2020
1 var object = { 'a': 1, 'b': 2, 'c': 1 };
2//This uses the package lodash
3
4  _.invert(object);
5  // => { '1': 'c', '2': 'b' }
6
7  // with `multiValue`
8  _.invert(object, true);
9  // => { '1': ['a', 'c'], '2': ['b'] }
10
11
queries leading to this page
flip keys and values of object javascriptjs reverse object keysjavascript reverse object key valuejs reverse object key valuejavascript how to print object value in reversereverse keys and values javascriptobject javascript switch values into keysinvert a key in js objectjavascript object keys in reversehow to use key value pair functionality reversible in javascriptjavascript object reverse keys and valuesreverse key with value object nodejsjs dictinary swap key and valuesswitch 28key 29 javascript keys and valuesobject reverse javascriptjavascript reverse key value objectjs flip object keys and valuesreverse object key and values javascriptjavascript swap object and keys valuesswap key value javascriptjavascript flip keys and valuesjavascript swap an object 27s keyjavascript map swap key valuereverse keys and values in object javascriptswtiching keys and values objecthow to swap position of keys of object in jsreverse object key and valuesreverse object keys javascriptswap keys and values javascripthow to reverse object in javascript by key valuehow to swap objects keys and values in javascriptjs object reverse key valuehow to reverse keys of object in javascriptrevert key and value in object jsswich key value object javascriptjavascript swap key value of objectinvert object keys and values javascriptreverse key value dictionary jsswap keys and values in an objectreverse key and value in object jsreverse key of object jsobject javascript swap values into keysreverse object keys jsjavascript invert objectjavascript dictionary flip key and valuesobject keys in reverse order javascripthow to reverse an object by values in javascriptjs reverse object by keyswap keys and values in an object javascriptjavascript invert key valuesjavascript reverse object properties and keysjavascript reverse object keyjavascript reverse object key valuejavascript swap object valuesjavascript swap key valueswap keys and values in object javascriptobject keys reversehow to swap position of key 27s of object in jsswitch js obj values to keysreverse js object key valueobject keys 28 29 reverse 28 29js reverse object keysjs object swap keys and valuesswitch keys and values javascript objectinvert values and keys json object javascriptobject keys reversereverse order of keys in object javascripthow to interchange object key value in javascriptjson flip key valueswap 22keys and values 22 in an object javascriptreverce object by key desc in jsjavascript flip object so that keys are values and values are keysjavascript object reverse key valuereverse key and value in object js