如何从当前索引中获取其他索引

时间:2019-08-31 13:19:33

标签: javascript dom

我是JavaScript的新手,请帮助我解决我遇到的问题。 我有一个数组,我想删除与当前索引不同的其他div的类列表。请帮助

我试图从其他讨论中找到答案,但我仍然没有得到想要的东西。

代码如下:

window.addEventListener("load", ()=> {
const sounds = document.querySelectorAll(".sound");
const pads = document.querySelectorAll(".pads div");
const visual = document.querySelector(".visual");

pads.forEach((pad, index) => {
    pad.addEventListener("click", function() {
        sounds[index].currentTime = 0;
        sounds[index].play();
        popUpNote();

        const ind = index + 1;
        //I Need to got the other index from the current index,
        //like if current index is 2, then i want to get 0,1,3,4,5
        //since i have 6 index inside
        pad.classList.add("active-pad"+ind);

        sounds[index].addEventListener("ended", function() {
            const noteChild = document.querySelector(".visual div");
            noteChild.remove();
            pad.classList.remove("active-pad"+ind);
        });
    });
});

const popUpNote = () => {
    const note = document.createElement("div");
    visual.appendChild(note);
    note.style.animation = 'beatOn 1.5s ease-in-out infinite both';
};

});

我想从当前索引中获取另一个索引,例如如果当前索引为2,那么我想得到0,1、3、4、5,因为我内部有6个索引

2 个答案:

答案 0 :(得分:0)

通过此方法,您可能会得到解决方案

在此JS脚本中,您将无法获取当前索引。

var arr = [1 , 2 , 3 , 4 , 5 , 6];

arr.forEach(function(valuw , index){
    console.log(valuw , index);
   for(var i = 0 ; i < arr.length ; i++){
    if( i != index){ 
    console.log("index at " , index , "itereator ===>" , i+1);
 }
}
});

答案 1 :(得分:0)

使用Array.filter()查找其他索引:

const idxs = [0,1,2,3,4,5,6,7,8,9,10]

const cur = 4

const res = idxs.filter(idx => idx !== cur)

console.log(res)

相关问题