如何在Forloop中使用动态键访问JS对象值

时间:2020-07-08 05:07:04

标签: javascript node.js json for-loop object

count定义下一个可用的q系列变量,因为代码计数为2,所以q8,q9可用。 提取我正在打印的q系列变量,如下所示:

let inputdata = {count:{value:2}, q8:{value:'John'}, q9:{value:'Siva'}};
    for (let index = 1; index <= inputdata.count.value; index++) {
        let pos = 7+index;
        let q = `inputdata.q` + pos;
        console.log("Vals: ", q.value);
    }

1 个答案:

答案 0 :(得分:0)

let inputdata = {count:{value:2}, q8:{value:'John'}, q9:{value:'Siva'}};
    for (let index = 1; index <= inputdata.count.value; index++) {
        let pos = 7+index;
        let q = 'q' + pos;
        console.log("Vals: ", inputdata[q]);
    }