Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中单击时循环数组_Javascript_Arrays - Fatal编程技术网

javascript中单击时循环数组

javascript中单击时循环数组,javascript,arrays,Javascript,Arrays,我想在每次单击时更改newAr的索引,并在控制台中记录looper函数中显示的newAr中下一个对象的值 注意:我只希望在第二次单击时索引第二个对象值,在第三次单击时索引newAr中的第三个对象值 HTML只有一个按钮,其他什么都没有 const button = document.querySelector("button"); let firstVar = () => { const firstVarVariable = Math.round(Math.ran

我想在每次单击时更改
newAr
的索引,并在控制台中记录looper函数中显示的
newAr
中下一个对象的值

注意:我只希望在第二次单击时索引第二个对象值,在第三次单击时索引
newAr
中的第三个对象值

HTML只有一个按钮,其他什么都没有

const button = document.querySelector("button");

let firstVar = () => {
  const firstVarVariable = Math.round(Math.random() * 10);
  if (firstVarVariable < 5) {
    return true;
  } else {
    return false;
  }
};
let secondVar = () => {
  const firstVarVariable = Math.round(Math.random() * 10);
  if (firstVarVariable < 5) {
    return true;
  } else {
    return false;
  }
};
let thirdVar = () => {
  const firstVarVariable = Math.round(Math.random() * 10);
  if (firstVarVariable < 5) {
    return true;
  } else {
    return false;
  }
};

const newAr = [];
const pusher = () => {
  newAr.push({
    one: firstVar(),
    two: secondVar(),
    three: thirdVar(),
  });
  console.log(newAr);
  looper();
};

const looper = () => {
  for (const value of newAr) {
    console.log(value.one);
    console.log(value.two);
    console.log(value.three);
  }
  // I want to change the index of newAr array on click
  // Like i want to console log first object in array on first click
  // and second object on other click and third object
  // alsp please tell me if i can change some approch
};

button.addEventListener("click", () => {
  pusher();
});
const button=document.querySelector(“按钮”);
让firstVar=()=>{
const firstVarVariable=Math.round(Math.random()*10);
if(firstVarVariable<5){
返回true;
}否则{
返回false;
}
};
让secondVar=()=>{
const firstVarVariable=Math.round(Math.random()*10);
if(firstVarVariable<5){
返回true;
}否则{
返回false;
}
};
让thirdVar=()=>{
const firstVarVariable=Math.round(Math.random()*10);
if(firstVarVariable<5){
返回true;
}否则{
返回false;
}
};
常数newAr=[];
常量推送器=()=>{
纽瓦推({
一:firstVar(),
二:secondVar(),
三:thirdVar(),
});
console.log(newAr);
活套();
};
常量循环器=()=>{
for(新AR的常量值){
console.log(值1);
console.log(值2);
console.log(值3);
}
//我想在单击时更改newAr数组的索引
//就像我想在第一次单击时控制台记录数组中的第一个对象一样
//然后单击另一个对象上的第二个对象和第三个对象
//请告诉我是否可以改变一些方法
};
按钮。addEventListener(“单击”,()=>{
推进器();
});
let randomBool=()=>Math.random()<0.5;
//这三个功能都做了同样的事情。。。
常数newAr=[];
常量推送器=()=>{
纽瓦推({
一:随机布尔(),
二:随机布尔(),
三:随机布尔(),
});
console.log(newAr);
活套();
};
设索引=0;//计数器/数组索引
常量循环器=()=>{
让value=newAr[index++];//始终获取下一个元素(从0开始)。
console.log(值1);
console.log(值2);
console.log(值3);
};
/***/
const button=document.querySelector(“按钮”);
按钮。addEventListener(“单击”,()=>{
推进器();
});
测试
let randomBool=()=>Math.random()<0.5;
//这三个功能都做了同样的事情。。。
常数newAr=[];
常量推送器=()=>{
纽瓦推({
一:随机布尔(),
二:随机布尔(),
三:随机布尔(),
});
console.log(newAr);
活套();
};
设索引=0;//计数器/数组索引
常量循环器=()=>{
让value=newAr[index++];//始终获取下一个元素(从0开始)。
console.log(值1);
console.log(值2);
console.log(值3);
};
/***/
const button=document.querySelector(“按钮”);
按钮。addEventListener(“单击”,()=>{
推进器();
});

Test
非常感谢,它最终帮助了我,从2天起,我的代码中出现了一些问题。非常感谢,它最终帮助了我,从2天起,我的代码中出现了一些问题。