Last active
May 6, 2019 15:05
-
-
Save mistercoffee66/5697ffd9e6bcb26e26bfbbcf70e75989 to your computer and use it in GitHub Desktop.
ES6 iterable Map example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
const els = document.querySelectorAll('li') | |
const iterables = new Map([ | |
['myObject', { | |
appetizer: 'Soup', | |
entree: 'Mahi Mahi', | |
dessert: 'Pie!' | |
}], | |
['myArray', [ | |
'avocado', | |
'tomato', | |
'onion', | |
'lime' | |
]] | |
]) | |
iterables.set('myCollection', els) | |
const iterate = (e) => { | |
const something = iterables.get(e.target.innerHTML) | |
console.log(something) | |
} | |
els.forEach((el) => { | |
el.addEventListener('click', iterate) | |
// click results: | |
// {appetizer: "Soup", entree: "Mahi Mahi", dessert: "Pie!"} | |
// ["avocado", "tomato", "onion", "lime"] | |
// [li, li, li] | |
}) | |
</script> | |
<div> | |
<h2>Are we having fun yet?</h2> | |
<ul> | |
<li>myObject</li> | |
<li>myArray</li> | |
<li>myCollection</li> | |
</ul> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment