-1

In my file I have my code:

for(var question in this.current.questionnaireProfil["p2"]){
    this.current=this.current.questionnaireProfil["p2"][question];
  }

and in my console.log :

this.current.questionnaireProfil ={p1: {…}, p2: {…}, p3: {…}, p4: {…}, p5: {…}, …}

Each time I click on next I'd like to go from p1 to p2, from p2 to p3, etc.

GalAbra
  • 5,048
  • 4
  • 23
  • 42
JohnFitze
  • 25
  • 6
  • Are all your properties labelled p1...pn ? if so you can simply have a string array defined before and update the index of the array when you press next. This will allow you to have a variable instead of "p2" as shown above and on update of that variable you can re run the for loop as per your need – BanelingRush Feb 27 '18 at 13:40
  • I have a json file (not editable) – JohnFitze Feb 27 '18 at 13:41
  • who looks like : "Profil": { "p1": { "Q1": { "intro" : "(Please note t... "intitule": "Home Phone:", "args": { "modalites" : { "1" : "numero fixe" } } } }, "p2" : { "Q2": { "intitule": "Mobile phone:", "args": { "modalites" : { "1" : "numero portable" } } } }, – JohnFitze Feb 27 '18 at 13:42
  • should p1 => click => p2 => click => p3......... – JohnFitze Feb 27 '18 at 13:43
  • okay in that case you want to go through all the keys of your JSON object [Iterating through JSON object](https://stackoverflow.com/questions/19323699/iterating-through-json-object-javascript) and store them in an array... then you want to set a handler to traverse the array on click [Go through an array on click](https://stackoverflow.com/questions/16549183/jquery-iterate-through-an-array-by-using-onclick) – BanelingRush Feb 27 '18 at 13:44
  • I tried I have this error : ERROR TypeError: Cannot read property 'length' of undefined – JohnFitze Feb 27 '18 at 13:57
  • you can't use a length property for JSON objects like that you have to go through the keys as shown above... please look through the threads and the answer below thoroughly and you will be able to do exactly what you need – BanelingRush Feb 27 '18 at 14:04
  • for (var i in this.current.questionnaireProfil) { if (this.current.questionnaireProfil.hasOwnProperty(i)) { console.log(i + " -> " + this.current.questionnaireProfil[key]); } } – JohnFitze Feb 27 '18 at 14:16
  • with that I have them all but I can not display any further than the p1 – JohnFitze Feb 27 '18 at 14:17

2 Answers2

0

You can create an array of the object's properties, then keep an index and show the object's property in this index:

var myObject = {
  questionnaire1: {
    1: "first",
    2: "second"
  },
  questionnaire2: {
    1: "first",
    2: "second",
    3: "third"
  },
  questionnaire3: {
    1: "first"
  }
};

// Create the array
var array = [];
for (property in myObject) {
  array.push(property);
}

var index = 0;
var out = document.getElementById("print");

function next() {
  var currentQuestionnaire = array[index % array.length];
  out.innerHTML = "myObject[" +
    currentQuestionnaire + "] contains " +
    Object.keys(myObject[currentQuestionnaire]).length + " questions";
  index++;
}
<button onclick="javascript:next();">Show next element</button>
<div id="print"></div>
GalAbra
  • 5,048
  • 4
  • 23
  • 42
  • no because it is necessary that if I am on the 5th group of question; it puts me the number of questions .. – JohnFitze Feb 27 '18 at 13:50
  • I'd have to make a loop so that when I click on next I go to the next question – JohnFitze Feb 27 '18 at 13:52
  • this.current.questionnaireProfil (console.log poster : {p1: {…}, p2: {…}, p3: {…}, p4: {…}, p5: {…}, …} ) – JohnFitze Feb 27 '18 at 13:53
  • @JohnFitze Please check my answer again. You didn't define what should happen on the last element, so I coded it to rotate (last > first > second etc.) – GalAbra Feb 27 '18 at 14:01
  • error /// `for(var question in this.current.questionnaireProfil["p2"]){ this.current=this.current.questionnaireProfil["p2"][question]; }` – JohnFitze Feb 27 '18 at 14:04
  • instead of "p2" would need some sort of function to say that I want the next question (ps: I'm already in my click function) – JohnFitze Feb 27 '18 at 14:05
  • define an array outside the onclick handler function with the values of all the keys of the JSON object and set a variable count to 0 inside the on Click handler you can do something like this `count++; for(var question in this.current.questionnaireProfil[arr[count]){ this.current=this.current.questionnaireProfil[arr[count]][question]; }` – BanelingRush Feb 27 '18 at 14:08
  • where arr is an array of strings of the key values of the JSON object that can be found here - [Iterating through JSON object](https://stackoverflow.com/questions/19323699/iterating-through-json-object-javascript) – BanelingRush Feb 27 '18 at 14:14
  • yes I get all in the console.log but how to do p1 => p2 .. p2 => p3 .. p3 => p4 ect ... ?? – JohnFitze Feb 27 '18 at 14:20
  • i just want to go from p1 to p2 ect .. no get back the property – JohnFitze Feb 27 '18 at 14:26
0
`for (var i in this.current.questionnaireProfil) {
if (this.current.questionnaireProfil.hasOwnProperty(i)) {
    console.log(i + " -> " + this.current.questionnaireProfil[key+1]);
};
}
for(var question in this.current.questionnaireProfil[i]){
this.current=this.current.questionnaireProfil[i][question];
}`

with this, I go from p1 to p32 (the last one for this group question).

JohnFitze
  • 25
  • 6