如何创建每个循环,但将数据保持在一起 [英] how to create an each loop, but keep the data together

查看:102
本文介绍了如何创建每个循环,但将数据保持在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个练习块,其中有一个word和4个syllables.在我的json中,它看起来像这样:

I have an exercise block with one word and 4 syllables. In my json it looks like this:

{
"main_object": {
"id": "new",
"getExerciseTitle": "TestFrontEnd",
"language": "nl_NL",
"application": "lettergrepen",
"main_object": {
  "title": "TestFrontEnd",
  "language": "nl_NL",
  "exercises": [
    {
      "word": "huisarts",
      "syllables": [
        "Huis",
        "arts",
        "",
        ""
      ]
    },
    {
      "word": "voetbal",
      "syllables": [
        "Voet",
        "bal",
        "",
        ""
      ]
    }
  ]
},
"dataType": "json"
 }
}

我想遍历这些wordsyllables,但是每个word必须在一个练习块中保留其syllables.现在,这就是我尝试这样做的方法,但我失败了很多时间:

I want to loop through these word and syllables, but each word has to remain with their syllables in one exercise block. Right now this is how I tried doing it and I failed big time:

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


 var exercisetitle = json.main_object.getExerciseTitle;
    $("#getExerciseTitle").val(exercisetitle);


 var exercise = json.main_object.main_object.exercises;
    $.map(exercise, function(exercise, i) {
        $("#addOpdracht").click();
        $(".exerciseGetWordInput_" + i).val(exercise.word) 
         console.log(exercise.syllables);
          $(".sylll" + i).val(exercise.syllables)
    });

  });

});

创建syllables输入的函数:

function getWordPartInput(id, cValue){
cValue = cValue || '';
var wpInput = $('<input/>', {
'class': 'form-group form-control syllable sylll' + TT ++,
'type': 'text',
'value': cValue,
'placeholder': 'Syllables',
'name': 'Syllablescounter['+ SyllablesID++ +']'
 });
return wpInput;
 }

为您可视化它,它应该看起来像这样

但它看起来像这样

那我该怎么做才能得到我想要的结果?

so what can I do to get the result I would like to have?

推荐答案

更新您的循环功能并查看

update you loop function like this and see

exercise.map(a=>{
    console.log("Word is "+a.word);
    a.syllables.map(b=>{
        console.log("Syllable is"+b)
    })
}
);

这是评论中提到的逻辑

这篇关于如何创建每个循环,但将数据保持在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆