【问题标题】:Alter array in loop and start at previously added item在循环中更改数组并从先前添加的项目开始
【发布时间】:2019-03-23 13:10:24
【问题描述】:

我想循环一个数组并添加到数组中,然后在循环内使用之前添加的项目。所以我想循环 1,4,1 并在 1 和 4 之间添加 0.5 并返回 1。所以最终的字符串或数组将是:

1 1.5 2 2.5 3 3.5 4等

问题是我可以添加到索引项,但不能添加到循环中的那个项。第一个循环是 1,然后我添加 1.5,第二个循环应该是 1.5,但循环将是 4。

base_string = '1,4,1';
base_string = base_string.split(',');

for (var i = 0; i < base_str.length; i++) {

// I add 1.5 to array and I want to use that the next loop, but the next loop is 4 instead

}

【问题讨论】:

  • 达到 4 后会发生什么?
  • 我必须回到 1 比如:3.5,3,2.5,2,1.5,1
  • 我需要将字符串 1 4 1 更改为 1 1.5 2 等,增量为 0.5

标签: javascript arrays string loops for-loop


【解决方案1】:

您可以减少给定的数组并通过取一个值进行递增或递减来填充值。

var string = '1,4,1',
    result = string
        .split(',')
        .map(Number)
        .reduce((r, v) => {
            if (!r) return [v];
            let last = r[r.length - 1],
                delta = v > last ? 0.5 : -0.5;
            while (last + delta !== v) r.push(last += delta);
            r.push(v);
            return r;
        }, undefined);
   
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 哇,这是一些严肃的编码......谢谢我稍后会研究它
【解决方案2】:

let base_string = '1,4,1';
let base_array = base_string.split(',').map(i => +i);
let inc = 0.5; // the number to add in every step
let res = [];

let i = 0;

// Iterate till the last element is reached
while(i < base_array.length) {
  // Iterate till there is some difference between adjacent items
  while(Math.abs(base_array[i] - base_array[i+1]) > inc) {
    res.push(base_array[i]);
    
    if(base_array[i] < base_array[i+1]) base_array[i] += 0.5;
    else base_array[i] -= 0.5;
  }
  
  i++;
}

res.push(base_array[i-1]); // add the last element

console.log(res.join(' '));

【讨论】:

  • 谢谢,但它给了我一个错误 i: Uncaught SyntaxError: Unexpected identifier: let i = 0, maxIter = 40, j = 0;
  • 对不起,格式不好,我已经用 cmets 的解释更新了它,它运行没有任何错误
  • 非常感谢。我花了一整天的时间,你拯救了我的一天,再次感谢
【解决方案3】:

这应该适用于任何数组。

var base_string = '1,4,1,7,2';
base_string = base_string.split(',');

var new_string = []; 
for(var i=0; i<base_string.length-1; i++){
   if(base_string[i+1]>base_string[i]){
     for(var j=parseInt(base_string[i]); j<parseInt(base_string[i+1]); j+=.5){
         new_string.push((j))
     }
   }
  else  if(base_string[i+1]<base_string[i]){
     for(var j=parseInt(base_string[i]); j>=parseInt(base_string[i+1]); j-=.5){
         new_string.push((j))
     }
   }
}
console.log("new_string: "+new_string);

【讨论】:

  • 谢谢你,我会调查的,但这有点过头了:)
【解决方案4】:

我认为您可以使用两个 for 循环来做到这一点,只需维护一个 direction 变量以将计数器指向正确的方向:

let arr = [1, 4, 1]
let res = [arr[0]]
let step = 0.5
for (let i = 1; i < arr.length; i++) {
  let direction = arr[i] > arr[i - 1] ? 1 : -1
  for (let j = (direction * arr[i - 1]) + step; j <= (direction * arr[i]); j += step) {
    res.push(j * direction)
  }
}
console.log(res)

【讨论】:

  • 是的,这行得通,非常感谢(它确实给了我像 1、1 这样的两位数,但我想我可以自己算出来)
  • 对,@Youss,已修复。谢谢。
【解决方案5】:

你可以像这样使用“reduce”函数:

var base_string = '1,2,3,4';
base_string = base_string.split(',').reduce((a, v, i, ar) => {
  if (i === 1) {
    // an array is created using the first two values of original the array
    return [+a, +a + 0.5, +v, +v + 0.5];
  }
  // after index 2 you add an extra item +0.5
  a.push(+v);
  a.push(+v + 0.5);
  return a;
});
console.log(base_string);

这将为您提供所需的输出:

[1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]

请注意,每个值之前都添加了一个加号(+),这是因为它们是字符串值,因此需要将它们转换为数字。

【讨论】:

  • 谢谢你,我会看看这个(减少对我来说是新的)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 2021-02-19
相关资源
最近更新 更多