reverse keys and values in object javascript

Solutions on MaxInterview for reverse keys and values in object javascript by the best coders in the world

showing results for - "reverse keys and values in object javascript"
Ilaria
27 Feb 2016
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}
Isabella
30 May 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
javascript swap object and keys valuesswtiching keys and values objectswap 22keys and values 22 in an object javascriptjavascript swap an object 27s keyjavascript reverse object properties and keysjavascript reverse object key valuehow to use key value pair functionality reversible in javascriptswitch 28key 29 javascript keys and valuesinvert a key in js objectswich key value object javascriptreverse key and value in object jsobject keys in reverse order javascripthow to swap objects keys and values in javascriptjs object reverse key valueswitch keys and values javascript objectreverse keys and values javascriptjavascript reverse object key valuejavascript flip keys and valuesjavascript object keys in reverseinvert values and keys json object javascriptjson flip key valuereverse object keys jsjs reverse object keysobject javascript switch values into keysjavascript reverse object keyflip keys and values of object javascriptreverse order of keys in object javascriptreverse object key and valuesobject javascript swap values into keysswap keys and values in an objectrevert key and value in object jsswap keys and values in an object javascriptswap keys and values in object javascriptjavascript invert key valuesjavascript map swap key valuereverse js object key valuejs reverse object key valuejs reverse object by keyhow to interchange object key value in javascriptjavascript reverse key value objectjs object swap keys and valuesreverse object key and values javascriptreverse key of object jsswap keys and values javascriptreverse object keys javascriptjavascript swap key value of objectjavascript how to print object value in reversereverse key with value object nodejshow to swap position of key 27s of object in jshow to swap position of keys of object in jshow to reverse object in javascript by key valuejavascript flip object so that keys are values and values are keysswitch js obj values to keysjs flip object keys and valuesjavascript invert objectjavascript dictionary flip key and valuesjavascript object reverse keys and valuesreverse keys and values in object javascriptjavascript object reverse key valuehow to reverse keys of object in javascriptobject reverse javascriptobject keys reverseobject keys reversejs dictinary swap key and valuesjavascript swap object valuesswap key value javascriptjavascript swap key valuereverse keys and values in object javascript