我想创建一个循环来浏览对象的每个属性:this.current.questionnaireProfil JavaScript

我想创建一个循环来浏览对象的每个属性:this.current.questionnaireProfil JavaScript,javascript,Javascript,在我的文件中,我有我的代码: for(var question in this.current.questionnaireProfil["p2"]){ this.current=this.current.questionnaireProfil["p2"][question]; } 在my console.log中: this.current.questionnaireProfil ={p1: {…}, p2: {…}, p3: {…}, p4: {…}, p5: {…}, …}

在我的文件中,我有我的代码:

for(var question in this.current.questionnaireProfil["p2"]){
    this.current=this.current.questionnaireProfil["p2"][question];
  }
在my console.log中:

this.current.questionnaireProfil ={p1: {…}, p2: {…}, p3: {…}, p4: {…}, p5: {…}, …}
每次单击“下一步”,我都希望从p1转到p2,从p2转到p3,等等。

您可以保留索引,然后在此索引中显示对象的属性:

var myObject={
问题1:{
1:“第一”,
2:“第二”
},
问题2:{
1:“第一”,
二:"第二",,
3.“第三”
},
问题3:{
1:“第一”
}
};
//创建数组
var数组=[];
用于(myObject中的属性){
array.push(属性);
}
var指数=0;
var out=document.getElementById(“打印”);
函数next(){
var currentQuestion=数组[索引%array.length];
out.innerHTML=“myObject[”+
当前调查问卷+“]包含”+
Object.key(myObject[CurrentQuestion])。长度+问题;
索引++;
}
显示下一个元素

这样,我从p1转到p32(这组问题的最后一个)。

您的所有属性都标记为p1…pn吗?如果是这样,您只需在之前定义一个字符串数组,然后在按“下一步”时更新该数组的索引即可。这将允许您使用一个变量,而不是如上所示的“p2”,并且在更新该变量时,您可以根据需要重新运行for循环。我有一个json文件(不可编辑),它看起来像:“Profil”:{“p1”:{“Q1”:{“intro”:(请注意t...“intitule”:“Home Phone:”,“args”:{“modalites”:{“1”:”数字固定“}}}”,p2:{“Q2”:{“intitule”:“Mobile:”,“args”:{“modalites”:{“1”:“数字便携”}}},如果p1=>click=>p2=>click=>p3………好的,在这种情况下,您希望遍历JSON对象的所有键并将它们存储在一个数组中…然后您希望设置一个处理程序,在单击no时遍历数组,因为如果我是第五组问题,这是必要的;它会给我带来问题的数量。我必须这样做一个循环当我点击next时,我进入下一个问题this.current.questionnaireProfil(console.log poster:{p1:{…},p2:{…},p3:{…},p4:{…},p5:{…},…})@JohnFitze请再次检查我的答案。你没有定义最后一个元素应该发生什么,所以我将其编码为旋转(最后一次>第一次>第二次等)错误///
(此.current.questionnaireProfil[“p2”]){this.current=this.current.questionnaireProfil[“p2”][question];}
`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];
}`