简体   繁体   English

使用while循环使用数组动态包装索引

[英]Dynamically wrap index with array using while loop

My code speaks for me, I want to wrap array index with another array dynamically (with a loop). 我的代码对我来说很重要,我想动态地(通过循环)用另一个数组包装数组索引。

The following code does not work. 以下代码不起作用。 Please, help me to convert this "x" string to JavaScript code or to find the right way to get the result. 请帮我将这个“ x”字符串转换为JavaScript代码,或者找到获取结果的正确方法。

var x = parentTasks[j];

while(x){
    x = parentTasks + '[' + numbers + '[' + x + ']]';
}

Later "x" will become undefined, so then loop should stop. 以后的“ x”将变得不确定,因此循环应停止。

What I expect: 我的期望:

Example when loop is iterated for 1st time: 第一次循环的示例:

parentTasks[numbers[parentTasks[j]]]

Example when loop is iterated for 2nd time: 第二次循环的示例:

parentTasks[numbers[parentTasks[numbers[parentTasks[j]]]]]

I did it by my self. 我是靠我自己做的。 Here is a solution: 这是一个解决方案:

var x = parentTasks[j];
var z = 0
while ( z++ < 2 ) {

    x = 'parentTasks[numbers[' + x + ']]';

    console.log(eval(x));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于起始索引在数组中循环/换行? - How to loop through/wrap around an array based on starting index? 循环遍历从动态生成的索引开始的数组 - Loop through an array starting at a dynamically-generated index 使用while循环遍历数组 - Using a while loop to loop over an array 如何使用 for in 循环动态填充数组 - How to dynamically populate an array using a for in loop 在 TypeScript 中使用 For 循环数组并动态加载项目 - Using For loop in TypeScript Array and load items dynamically 使用获取时如何在for循环中动态遍历多个Promise? - How to loop dynamically over multiple promises in a for loop while using fetch? 创建对象数组并在Javascript的while循环中动态实例化它们 - Creating an array of objects and dynamically instantiating them in a while loop in Javascript 使用 ngFor 循环的索引动态设置类或 id - Dynamically set class or id using index of ngFor loop gatsby/react中多维数组上的map()方法,尝试在div中包装内部循环时出错 - map() method on multi dimensional array in gatsby/react, getting error while trying to wrap inner loop in div 如何使用while循环和jquery动态更改菜单栏的活动CSS? - How to dynamically change active css for menubar using while loop and jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM