简体   繁体   English

每次点击可增加数组中的一个索引

[英]increment by one index in array with each click

I'm just getting hands on JavaScript and I can't figure out how to increment by 1 the index selector with each click. 我只是接触JavaScript而已,我不知道如何在每次单击时将索引选择器加1。

The following code is giving me the index 6, but I would like this index to increment with each click: 以下代码为我提供了索引6,但我希望该索引随着每次点击而增加:

var nextCompanyLabel = 6;

label: label[nextCompanyLabel++],

This is the complete code: 这是完整的代码:

$('#addDataset').click(function() {
  var background = randomColor(0.5);
  var nextCompanyLabel = 6;
  var newDataset = {
    label: label[nextCompanyLabel++],
    borderColor: background,
    backgroundColor: background,
    pointBorderColor: background,
    pointBackgroundColor: background,
    pointBorderWidth: 1,
    fill: false,
    data: [],
  };

  for (var index = 0; index < config.data.labels.length; ++index) {
    newDataset.data.push(randomScalingFactor());
  }

  config.data.datasets.push(newDataset);
  window.myLine.update();
});

All help will be appreciated! 所有帮助将不胜感激!

you are reinitialzing nextCompanyLabel to 6 on each click, so incrementation doesn't hold. 您会在每次点击时将nextCompanyLabel重新初始化为6,因此增量将不成立。 move the initialization statement outside the event handler function. 将初始化语句移到事件处理函数之外。

var nextCompanyLabel = 6;  //reinitializing nextCompanyLabel, remove this line.
  var newDataset = {
    label: label[nextCompanyLabel++],

You are getting the answer 6 each time because, each time the user clicks, the variable, var nextCompanyLabel is explicitly given the value of 6. ie, 每次得到的答案都是6,因为每次用户单击时,变量var nextCompanyLabel被明确赋予值6。即,

 var nextCompanyLabel = 6; 

Later when you are incrementing it you are using postfix increment, so your value is always 6. 以后在递增时,使用的是后缀递增,因此您的值始终为6。

Either declare var nextCompanyLabel = 6; 要么声明var nextCompanyLabel = 6; outside the function or make it static. 函数外部或使其静态。

This should solve your problem. 这应该可以解决您的问题。

You could try initializing your nextCompanyLable variable outside the click function like so: 您可以尝试在click函数之外初始化nextCompanyLable变量,如下所示:

var nextCompanyLabel = 6;
$('#addDataset').click(function() {
  var background = randomColor(0.5);
  var newDataset = {
    label: label[nextCompanyLabel++],
    borderColor: background,
    backgroundColor: background,
    pointBorderColor: background,
    pointBackgroundColor: background,
    pointBorderWidth: 1,
    fill: false,
    data: [],
  };

  for (var index = 0; index < config.data.labels.length; ++index) {
    newDataset.data.push(randomScalingFactor());
  }

  config.data.datasets.push(newDataset);
  window.myLine.update();
});

Assuming your label array to be defined somewhere before in the code, you should initialize the nextCompanyLabel outside the callback function, the updating its value on every click event, in a way such that the index doesn't go out of the array's bounds (for instance using the modulo "%" operator): 假设您的标签数组在代码之前的某个位置定义,则应在回调函数之外初始化nextCompanyLabel ,并在每次单击事件时更新其值,以使索引不会超出数组的范围(对于模“%”运算符的实例):

 var nextCompanyLabel = 6; $('#addDataset').click(function() { var background = randomColor(0.5); nextCompanyLabel = (nextCompanyLabel + 1) % label.length; var newDataset = { label: label[nextCompanyLabel], borderColor: background, backgroundColor: background, pointBorderColor: background, pointBackgroundColor: background, pointBorderWidth: 1, fill: false, data: [], }; for (var index = 0; index < config.data.labels.length; ++index) { newDataset.data.push(randomScalingFactor()); } config.data.datasets.push(newDataset); window.myLine.update(); }); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM