0

Yes, I have done my research and found out about this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split or the possible duplicate people could refer to: How to split comma separated string using JavaScript?

however my question now is: When I fetch my syllables (I'll be right back on what that is) I end up with commas between each word I have. I would like to escape these commas so the syllables go into their own input field instead of becoming one string in one input field.

This is how my loop looks like right now:

 $(document).ready(function () {
 $.getJSON('json_files/jsonData_' + ID + '.json', function(json) {

//Fetches the correct exercise title from the database and places it inside the header
    var exercisetitle = json.main_object.getExerciseTitle;
    $("#getExerciseTitle").val(exercisetitle);

//loops through the words and fetches them back to the CMS side.
var exercise = json.main_object.main_object.exercises;
    $.map(exercise, function(exercise, i) {
        $("#addOpdracht").click();
        $(".exerciseGetWordInput_" + i).val(exercise.word) // starts with 0
         console.log(exercise.syllables);
          $(".sylll" + i).val(exercise.syllables)


    });

  });

});

however when I use this loop I end up with the words in one input field with commas in between and therefore it fetches back all the words in one input field.

How my JSON looks like:

{
"main_object": {
"id": "new",
"getExerciseTitle": "TestToConfirmMoreWords",
"language": "nl_NL",
"application": "lettergrepen",
"main_object": {
  "title": "TestToConfirmMoreWords",
  "language": "nl_NL",
  "exercises": [
    {
      "word": "HACCP",
      "syllables": [
        "1",
        "2",
        "",
        ""
      ]
    },
    {
      "word": "HACCP-normen",
      "syllables": [
        "123",
        "123",
        "123",
        ""
      ]
    }
  ]
},
"dataType": "json"
}
}

And here is a picture so it's easier to visualize:

enter image description here

as you can see it puts "1,2,," in one input field instead of 2 input fields and the 2 last input fields empty. It also just stacks "123,123,123," underneath it in one input field, instead of placing it in the right exercise field with each in their respective input field. The last one should again, be empty since that's an "empty" string.

Is it me who's looking in the wrong direction (e.a looking at the split function while that won't solve the issue) or is it something else that I am missing out on?

D.Sof
  • 119
  • 1
  • 10
  • `split` will take a string `"1,2,3"` and give you an array `[1],[2],[3]` (or so...). Here, you *already have* an array, but you're outputting the array so js is converting it to a comma separated string for you. You need to loop through syllables in a similar way to how you loop through exercises. – freedomn-m Jun 27 '18 at 09:17
  • Crap... so the post I made is useless..? Is there a way to join the syllables with the word? or do I have to double loop ? – D.Sof Jun 27 '18 at 09:21
  • It's not clear what your desired outcome is (what you're trying to achieve). Might be as simple as `exercise.word + ' ' + exercise.syllables` ? – freedomn-m Jun 27 '18 at 09:44
  • You see in my json words with syllables, each word has its own respective syllables. What I am trying to achieve is each word (that I fetch back) should be combined with it's syllables. for example: word: HACCP with the syllables: 1 and 2... 1 should be in the first syllable input under the word HACCP and 2 in the second under HACCP and the last 2 should remain empty (they never got a value). and then the same goes for HACCP-normen and its syllables. I thought the split could do this and therefore the post – D.Sof Jun 27 '18 at 10:01

0 Answers0