Javascript:For循环中的动态变量

时间:2018-08-30 13:48:44

标签: javascript jquery variables for-loop dynamic

我是JavaScript初学者,需要您的帮助!非常感谢您的帮助!

cell_660 = cell_638;
cell_659 = cell_637;
...

以这种方式工作。我现在想每秒对所有660个变量执行此操作,以将其值更改为名称末尾带有数字(自己的数字为-22)的变量的值。当然无需编写660行代码!到目前为止,我已经尝试过:

var count = 660;

setInterval(function(){

for(var i=1;i<=660;i++){
    'cell_' + count = 'cell_' + eval(count - 22);
    count--;
}
count=660;

},1000);

我该如何正确编写?我已经读过有关window ['cell_'+ count]的信息-但我不想每秒创建660个新变量。我想每秒更改660个变量的值。

3 个答案:

答案 0 :(得分:1)

您是否需要每秒更改660个变量?如果仅创建一个数字变量以显示或使用数组值时保持“数组中要开始的位置”的位置怎么办?

var cells = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

var currentStart = 0;

// create an interval to modify currentStart
setInterval(function () {
  currentStart = (currentStart + 22) % cells.length;
}, 1000);

function doSomething() {
  // display contents starting at currentStart
  var msg = "";
  for (var i = 0; i < cells.length; i++) {
    var index = (i + currentStart) % cells.length;
    // do something with cells[index]
    msg += "," + cells[index];
  }
  console.log(msg);
}

document.querySelector("#thing").addEventListener("click", doSomething);
<button id='thing'>click me</button>

答案 1 :(得分:0)

带有数组

拥有一个数组而不是这么多的变量怎么样?

let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

function moveArrElem() {
  // loop all elements of the array
  // using arr.map give us a buffer so current index are keeped while doing the modification
  arr = arr.map((_, i) => {
    
    // get the new index and make it valid if necessary
    let newIndex = i - 2; 
    if (newIndex < 0) {
      newIndex += arr.length
    }
    return arr[newIndex]
  })
  console.log(arr)
}

console.log(arr)
setInterval(moveArrElem, 2000)

大约window['cell_' + count]不会每次都创建新变量,实际上var test = 42等于window["test"] = 42(如果您位于全局范围内(任何函数和其他函数之外)块)

所以window['cell_' + count]只会修改var的值

var test = 42
console.log(window["test"])

最终尝试尽可能多地避免使用eval,这很慢并且通常是安全问题

(其中有一些)对eval的呼叫几乎总是表明在其他地方存在误解


没有数组

由于OP在评论中告诉他他仍然还没有学习数组,所以这是没有数组的解决方案

let quantity = 660

// declaring the 660 vars
for (let i = 0; i < quantity; i++) {
  window["cell_" + i] = i;
}

function moveCells() {
  // need some tmp vars
  for (let i = 0; i < quantity; i++) {
    window["tmp_" + i] = window["cell_" + i];
  }
  
  for (let i = 0; i < quantity; i++) {
    let newIndex = i - 22
    if (newIndex < 0) {
      // equals to : newIndex = newIndex + quantity
      newIndex += quantity
    }
    window["cell_" + newIndex] = window["tmp_" + i];
  }

  // displaying 660 items would take to much time and place
  console.log("cell_42 : " + cell_42)
  console.log("cell_43 : " + cell_43)
  console.log("cell_44 : " + cell_44)
  console.log("cell_45 : " + cell_45)
  console.log("cell_46 : " + cell_46)
}

console.log("cell_42 : " + cell_42)
console.log("cell_43 : " + cell_43)
console.log("cell_44 : " + cell_44)
console.log("cell_45 : " + cell_45)
console.log("cell_46 : " + cell_46)

setInterval(moveCells, 1000)

答案 2 :(得分:0)

在给您答案之前,我首先要指出这是糟糕的代码实践。您永远不需要660个变量!无论您要做什么,都可以通过数组来完成。话虽如此,这是您如何进行这项工作,以及如何在下面实际进行这项工作。我强烈建议您考虑使用数组方法!

setInterval(function() {
  for(var i = 660; i > 22; i--) {
    window['cell_' + i] = window['cell_' + (i - 22)];
  }
},1000);

现在,这实际上是您实际应该做的事情。

var cells = [/* 660 elements */];
setTimeout(function() {
  for(var i = 22; i < 660; i++) {
    cells[i] = cells[i - 22];
  }
}, 1000);

实际上,使用此方法,您可以使用ES6中新的Array#copyWithin方法将整个事情集成在一起。

const cells = [/* 660 elements */];
setTimeout(() => {
  cells.copyWithin(22);
}, 1000);
相关问题