使用ForEach循环递增的值应用于每个元素 [英] Incrementing value with a ForEach loop to apply on each element
问题描述
寻找有关数组和增量的帮助.
looking for help with arrays and increments.
我有一个函数,可以将相同的样式应用于数组中的每个新元素,如下所示.
I have a function which applies the same style to each new element in an array as you can see below.
function SnakeBodyLeft(){
StorePositions.forEach(BodySnake => {
BodySnake.style.gridRowStart = (Initial_y + y);
BodySnake.style.gridColumnStart = (Initial_x + x)+1 ;
});
};
// this results in
StorePositions[0] = BodySnake.style.gridColumnStart = (Initial_x + x)+1
StorePositions[1] = BodySnake.style.gridColumnStart = (Initial_x + x)+1
StorePositions[2] = BodySnake.style.gridColumnStart = (Initial_x + x)+1
And so on...
我想编写一个函数,执行上面所示的操作,但是为每个新元素增加值+1,并为第一个元素保留初始值.结果可能是这样的:
I want to write a function that does what is shown above , but increments the value +1 for Each new element and keep the initial value for the first elements. The result would be somethink like :
StorePositions[0] = BodySnake.style.gridColumnStart = (Initial_x + x)+1
StorePositions[1] = BodySnake.style.gridColumnStart = (Initial_x + x)+2
StorePositions[2] = BodySnake.style.gridColumnStart = (Initial_x + x)+3
StorePositions[3] = BodySnake.style.gridColumnStart = (Initial_x + x)+4
And so on....
我试图使1称为i的变量并使之递增.但是,当我这样做时,i的增量将应用于数组中的所有元素,而不仅是新元素.
I have tried to make 1 a variable called i and increment it. But when I do this , the increment of i is applied to all elements in the array and not just the new elements.
let i = 1++;
function SnakeBodyLeft(){
StorePositions.forEach(BodySnake=> {
BodySnake.style.gridRowStart = (Initial_y + y);
BodySnake.style.gridColumnStart = (Initial_x + x) + i ;
});
};
//this results in the incremented value being applied to all elements , not just the new ones .
StorePositions[0] = BodySnake.style.gridColumnStart = (Initial_x + x)+1
StorePositions[1] = BodySnake.style.gridColumnStart = (Initial_x + x)+1
StorePositions[2] = BodySnake.style.gridColumnStart = (Initial_x + x)+1
or
StorePositions[0] = BodySnake.style.gridColumnStart = (Initial_x + x)+2
StorePositions[1] = BodySnake.style.gridColumnStart = (Initial_x + x)+2
StorePositions[2] = BodySnake.style.gridColumnStart = (Initial_x + x)+2
基本上,随着索引的增长(0,1,2,3 ...),索引中每个新元素的值i也应增长(0,1,2,3 ...).
Basically , as the index grows(0,1,2,3...) , so should the value i in each new element in the index.(0,1,2,3...).
但是Iam有点卡住了!任何人都可以帮忙吗?
But Iam kinda stuck ! Anyone can help ?
推荐答案
尝试一下
function SnakeBodyLeft(){
StorePositions.forEach((BodySnake, i)=> {
BodySnake.style.gridRowStart = (Initial_y + y);
BodySnake.style.gridColumnStart = (Initial_x + x) + i;
});
};
更新如果您需要从1开始递增,请执行以下操作:
UPDATE If you need to start the increment from 1 then do this instead:
function SnakeBodyLeft(){
StorePositions.forEach((BodySnake, i)=> {
BodySnake.style.gridRowStart = (Initial_y + y);
BodySnake.style.gridColumnStart = (Initial_x + x) + (i + 1);
});
};
您可以为forEach添加一个索引,例如 forEach(element,index)=>{...}
You can add an index for the forEach like so forEach(element, index) => { ... }
这篇关于使用ForEach循环递增的值应用于每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!