Skip to content

Instantly share code, notes, and snippets.

@mistercoffee66
Last active May 6, 2019 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mistercoffee66/5697ffd9e6bcb26e26bfbbcf70e75989 to your computer and use it in GitHub Desktop.
Save mistercoffee66/5697ffd9e6bcb26e26bfbbcf70e75989 to your computer and use it in GitHub Desktop.
ES6 iterable Map example
<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