Invert key value in js object
Posted: 28 Oct 2020, 09:13
I can't figure out how I can change the following object in JavaScript:
to:
I want to associate unique values with list of containing keys in a JavaScript object.
Here is what I have tried:
Code: Select all
{"first":["de"], "second":["ab","de"], "third":["de"]}
Code: Select all
{"de":["first", "second", "third"], "ab":["second"]}
Here is what I have tried:
Code: Select all
const data = {
"first":["de"],
"second":["ab","de"],
"third":["de"]
}
console.log(
Object
.keys(data).reduce(function(obj, key) {
obj[data[key]] = key;
return obj;
}, {})
)