Let's suppose that we have this array:
javascriptlet foodArray = [ ['🍈', 'Melon'], ['🍉', 'Watermelon'], ['🌭', 'Hot dog'], ['🥓', 'Bacon'] ];
We want to convert it into an object like that:
javascriptlet foodObject = { '🍈': 'Melon', '🍉', 'Watermelon', '🌭', 'Hot dog', '🥓', 'Bacon' }
We can use the Object.fromEntries()
function to transform fruitArray
into fruitObject
:
javascriptlet foodArray = [ ['🍈', 'Melon'], ['🍉', 'Watermelon'], ['🌭', 'Hot dog'], ['🥓', 'Bacon'] ]; let foodObject = Object.fromEntries(fruitArray);
That's it!
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.