I have this function here:
var obj = {
name: 'Holly',
age: 35,
role: 'producer'
};
function convertObjectToList(obj) {
return Object.keys(obj).map(k => [k, obj[k]]);
}
convertObjectToList(obj);
This function converts an array to obj. So if I have that obj above i'll get something like this:
[['name', 'Holly'], ['age', 35], ['role', 'producer']]
Now I want to focus here:
return Object.keys(obj).map(k => [k, obj[k]]);
Breaking it down, Object.keys basically returns an array of a given object's own enumerable properties and map iterates in an array and so something with it. I am trying to understand the arrow function above and try to break it down into simpler understandable code for me.
function convertObjectToList(obj) {
Object.keys(obj).map(function(key){
obj = [key, obj[key]];
});
console.log(obj);
}
But this one did not work. Instead it only returns ["role", undefined].
Is there anyone out there who can make me understand in laymans term and break down the code so I would understand it clearly.
Sorry I am beginner. Noob.