title: JS - Index of value; Arrays
body: <p>Might be a simple question for some, but im currently stuck here.</p>
  <p>What i'm trying to create is a quiz, that grabs the questions, answers, solutions and the type of question from a JSON.<br/>
  So far, it works and i get an array with the answers selected and removing the answers that are unselected. But the array gets filled with the id, but i actually want it's answerList index.</p>
  <pre><code>   {
      &quot;quid&quot;:1,
      &quot;question&quot;: &quot;question 1&quot;,
      &quot;answerList&quot;:[&quot;answer 1_1&quot;,&quot;answer 1_2&quot;],
      &quot;solution&quot;:[1],
      &quot;type&quot;:&quot;checkbox&quot;
     }
  </code></pre>
  <p>This is what my JSON looks like. When running the code below, and checking both boxes, i get an array as [&quot;answer1_1&quot;, &quot;answer1_2&quot;]. But i need an array like [0, 1] because i later on want to compare it to the solution, to see if the selection was correct or not. Also, since there are mulitple checkbox questions (i removed everything else since it's irelevant here), it gets all pushed into the same array. Ideally i'd get arr1=[0, 1], if both were selected in this case and arr=[0, 1, 2] for the next.<br/>
  Maybe i've been sitting here for too long to not see the obvious, simple answer to my question.</p>
  <p>Anyway, here is a code snippet.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//Parse JSON
  let quizDyn = JSON.parse(`[
      {
          "quid":1,
          "question": "question 1",
          "answerList":["answer 1_1","answer 1_2"],
          "solution":[1],
          "type":"checkbox"
  
      }, {
          "quid":2,
          "question": "question 2",
          "answerList":["answer 2_1","answer 2_2","answer 2_3"],
          "solution":[0,1,2],
          "type":"checkbox"
      }, {
          "quid":3,
          "question": "question 3",
          "answerList":["answer 3_1","answer 3_2","answer 3_3","answer 3_4"],
          "solution":[0],
          "type":"checkbox"
      }
  ]`)
  
  // just creating a div here
  var spalteQuiz = document.createElement("div")
  spalteQuiz.setAttribute("class", "spalteQuiz")
  document.body.append(spalteQuiz)
  
  
  for (var i = 0; i &lt; quizDyn.length; i++) {
  
    var neuesQuiz = document.createElement("div")
    neuesQuiz.setAttribute("class", "quiz")
    var aktuellesQuiz = document.getElementById("pOut")
  
    neueFrage = document.createElement("p")
    neueFrage.setAttribute("class", "frage")
    neueFrage.setAttribute("id", "frage " + (i + 1) + " frage")
    neueFrage.setAttribute("name", "frage " + (i + 1) + " frage")
    neueFrage.textContent = quizDyn[i].question
    var aktuelleFrage = document.getElementById("divOut")
  
    spalteQuiz.append(neuesQuiz)
    neuesQuiz.append(neueFrage)
  
    var frageTyp = quizDyn[i].type
    var anzahlAntworten = quizDyn[i].answerList.length
    var quidi = quizDyn[i].quid
    var quid = quizDyn.quid
    var entferneLeerzeichen = str =&gt; str.replace(/[\s,.]/g, '')
  
  
    // variable used for my array
    var loesung = []
  
    // function that's used for questions that are to be answered with checkboxes
    function istCheckbox() {
  
      //creating the &lt;form&gt;'s 
      var form = document.createElement("form")
      form.setAttribute("name", "frage" + quidi + "form")
      neuesQuiz.append(form)
  
      // creating the &lt;input&gt;'s
      for (let i = 0; i &lt; anzahlAntworten; i++) {
  
        checkbox = document.createElement("input")
        checkbox.setAttribute("type", "checkbox")
        checkbox.setAttribute("name", "frage" + quidi + "checkbox")
        checkbox.setAttribute("id", entferneLeerzeichen(quizDyn[quidi - 1].answerList[i]))
  
  
        // creating the &lt;label&gt;'s
        var label = document.createElement("label")
        label.textContent = quizDyn[quidi - 1].answerList[i]
        label.setAttribute("for", entferneLeerzeichen(quizDyn[quidi - 1].answerList[i]))
  
        // here i check if the checkbox is checked
        checkbox.addEventListener('change', function() {
  
          // if it's checked, push the checkboxes id to the array
          if (this.checked) {
            loesung.push(this.id)
          } else {
            // when unchecking, find the index of the unchecked checkbox'S id
            index = loesung.indexOf(this.id)
            // splice 1 object at the position of index
            loesung.splice(index, 1)
          }
          // console log the array
          console.log(loesung)
        })
  
        // appending it all
        var zeilenUmbruch = document.createElement("br")
        form.append(checkbox)
        form.append(label)
        form.append(zeilenUmbruch)
      }
    }
  
    //there is more usually, because irelevant
    function istTyp() {
      istCheckbox()
    }
  
    istTyp()
  }</code></pre>
  </div>
  </div>
  </p>