Home  >  Q&A  >  body text

javascript - About the problem that the name of the nested object in js is a variable

I defined a nested loop in angular2 to display a form. Now I want to process the data entered by the user. The code is as follows

this.data = value;
for (var i=0;i<this.questionlist.length;i++){
    this.questionId[i]=this.questionlist[i].id;
    this.tempdata = this.questionlist[i].ans;
    for(var j=0;j<this.tempdata.length;j++){
        console.log(this.data[this.questionId[i]].(this.tempdata[j]))
    }
}

The names of each array and the arrays nested inside are the methods of obtaining the data before. Generally speaking, they are not fixed. Now I want to read out each value, so the question is, what should I do? How can I read the nested value where the last this.tempdata[j] is the name?
I’ve been thinking about it for a long time and checking it out for a long time, but I can’t find any solution_(:з ∠)_

習慣沉默習慣沉默2647 days ago634

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-06-26 10:53:55

    Can you write a mock of your data? It’s very abstract. Look at the data and tell everyone which one and which layer you want to get. It will be easier to understand

    Is this what you want

    this.data[this.questionId[i]][this.tempdata[j]]
    

    Is this how to take the this.tempdata[j] attribute of this.questionId[i] under this.data (it is still an object here)?

    reply
    0
  • Cancelreply