In my coding challenge, I am trying to find a way to match the keys from an Object to elements in an array and replace the matching elements in the array with the corresponding key values.
Here is what I currently have:
Arr = [ 'Saturday', 'niCCCght', 'plans,', 'CCC' ]
Obj = { "AAA": "BBB", "CCC": "DDD", "EEE": "FFF" }
After running my code, I am getting the following output:
[
[ 'Saturday', 'Saturday', 'Saturday' ],
[ 'niCCCght', 'niCCCght', 'niCCCght' ],
[ 'plans,', 'plans,', 'plans,' ],
[ 'CCC', 'DDD', 'CCC' ]
]
But what I really want is this desired output:
[ 'Saturday', 'niCCCght', 'plans,', 'DDD' ]
Any help on achieving this would be greatly appreciated. Thank you!
function test() {
let Arr = [ 'Saturday', 'niCCCght', 'plans,', 'CCC' ];
const Obj = {"AAA":"BBB", "CCC":"DDD","EEE":"FFF"};
const Z = Arr.map(v => matchedKey(Obj,v))
console.log(Z);
}
let matchedKey = (Obj,str) => Object.keys(Obj).map(key => key===str ? Obj[key]: str);