Skip to content Skip to sidebar Skip to footer

How To Create An Each Loop, But Keep The Data Together

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', 'a

Solution 1:

Here is some JavaScript solution for your problem defined in question title:

var json = {
	"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"
	}
}

var exercise = json.main_object.main_object.exercises;

exercise.forEach(function(ex) {
       console.log("For the word " + ex.word + " the syllabus are:");
	   ex.syllables.forEach(function(sy, i) {
		console.log(i+1 + " " + sy);
	   
		})
})

Solution 2:

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)
    })
}
);

this is the logic mentioned in the comment

Post a Comment for "How To Create An Each Loop, But Keep The Data Together"