数组可以存储在JavaScript函数中吗?

时间:2018-08-11 04:08:08

标签: javascript arrays function

我很好奇,数组可以存储在函数中吗?当我在函数中声明数组时。由于无法读取该值,因此会出错,一旦我将其从函数中取出,便可以读取该值。

function decades(e) {
  var decadesArray = ["one decade",
  "two decades",
  "three decades",
  "four decades",
  "five decades",
  "six decades",
  "seven decades",
  "eight decades",
  "nine decades",
  "millennium"];

  for( var i = 0; i <= 100; i++) {
    if (i % 10 == 0) {
      console.log("decadesArray[0]");
    }
  }
}

十年(i);

4 个答案:

答案 0 :(得分:0)

您想要做的是在if语句为true时,在shift / splice的if语句内部删除一个数组元素。在您的情况下,您每次都会移动第一个数组元素。

function decades(e) {
  var decadesArray = ["one decade",
  "two decades",
  "three decades",
  "four decades",
  "five decades",
  "six decades",
  "seven decades",
  "eight decades",
  "nine decades",
  "millenium"];

  for( var i = 0; i <= 100; i++) {
    console.log("Year: ",i);
    //check if i is greater than 0
    //otherwise when i is 0 will be considered
    //the first decade
    if (i % 10 == 0 && i>0) {
      console.log( decadesArray.shift(), i );
    }
  }
}
decades();console.log( decadesArray[i/10] || i );

如果您不想实际提取数组的每个元素,则可以使用(i-10)/10作为数组的索引并打印出来,如果该索引最终为undefined,则只需打印数字即可。

function decades(e) {
  var decadesArray = ["one decade",
  "two decades",
  "three decades",
  "four decades",
  "five decades",
  "six decades",
  "seven decades",
  "eight decades",
  "nine decades",
  "millenium"];

  for( var i = 0; i <= 100; i++) {
    console.log( decadesArray[(i-10)/10] || i );
  }
}
decades();

答案 1 :(得分:0)

这是您要尝试的吗?如果没有,请尝试更加清楚。

请注意,在StackOverflow片段控制台中。显示的最大行数是有限的。这就是为什么输出看起来像五十年代数字那样开始的原因。

function decades() {
  var decadesArray = ["one decade",
  "two decades",
  "three decades",
  "four decades",
  "five decades",
  "six decades",
  "seven decades",
  "eight decades",
  "nine decades",
  "millennium"];

  for( var i = 0; i <= 100; i++) {
    if (i % 10 == 0 && i > 0) {
      console.log(decadesArray[Math.trunc(i/10)-1]);
    } else {
      console.log(`Year: ${i}`)
    }
  }
}
decades();

答案 2 :(得分:0)

另一种反转循环的方法。循环数十年,并在其中循环10次。在阵列上使用slice()可以在1到10年的时间里进行显示

function decades(e=10) {
  var decadesArray = ["one decade",
    "two decades",
    "three decades",
    "four decades",
    "five decades",
    "six decades",
    "seven decades",
    "eight decades",
    "nine decades",
    "millennium"
  ];
  decadesArray.slice(0,e).forEach(function(text, di) {
    var start = di * 10 + 1;
    for (var i = start; i < start + 10; i++) {      
        console.log('year: ', i)
    }
    console.log(text);
  });
}

decades()

答案 3 :(得分:-1)

Javascript函数仅在被调用时才会初始化,函数内部的所有私有变量仅在该时间被激活。

您需要在函数外部分配变量,以将存储的分配值保留在变量中。

var decadesArray = ["one decade",
  "two decades",
  "three decades",
  "four decades",
  "five decades",
  "six decades",
  "seven decades",
  "eight decades",
  "nine decades",
  "millenium"
];

decades(0);

function decades(e) {

  decadesArray.push("New value stored 1");
  decadesArray.push("New value stored 2");
  decadesArray.push("New value stored 3");


  for (var i = 0; i < decadesArray.length; i++) {
    console.log(decadesArray[i]);
    document.getElementById('logVw').innerHTML += decadesArray[i] + '<br>';
  }
}
<div id='logVw'>
</div>