I'm working on a code quiz and used the map function to iterate through an array of objects which each contain a title, choices and an answer for each question. I used a template literal to make the code easily readable. The key in the map function has the correct value when the button is created, however, when I click the button it always returns 0.
function printIt(input){
console.log(input);
}
var question = `
<div class='title'>
<h1>${questions[cur].title}</h1>
<p>${questions[cur].choices.map((item, i) => `
<button class='answers' id='${i}' onclick='${printIt(i)}'>${item}</button><br />
`).join('')}</p>
</div>
`;
//put on page
$("#container").append(question);