JavaScript
Write a function named "reverse_kvs" that has a key-value store as a parameter and returns a new key-value store. Your function should add each key-value pair in the parameter to the new key-value store EXCEPT reversing the key and value. For example, if the key-value "extreme":45 were in the parameter then the returned key-value store should contain the key-value pair 45:"extreme".
CODE:-
var obj = {"extreme":45,"hi":5}
function convertObject(obj) {
var obj2 = {};
// Get keys from obj
var keys = Object.keys(obj)
// Get values from obj
var values = Object.values(obj)
//Old object
console.log(obj)
for(var i=0;i<keys.length;i++){
// Assigning to new Object
obj2[values[i]] = keys[i]
}
//New Object
console.log(obj2)
}
convertObject(obj);
OUTPUT:-
CODE SCREENSHOT:-
If you have any queries, please ask me...Thank you...If you like the answer, please rate our answer.
Get Answers For Free
Most questions answered within 1 hours.