简体   繁体   English

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

[英]How to get other index from the current index

i'm a newbie on JavaScript, help me to fix the problem i got.我是 JavaScript 新手,请帮助我解决遇到的问题。 I have an array, i want to remove class list for other div which are not same with the current index.我有一个数组,我想删除与当前索引不同的其他 div 的类列表。 Please help请帮忙

i tried to find the answer from other discussion, but i still don't get what i want.我试图从其他讨论中找到答案,但我仍然没有得到我想要的。

Here's the code:这是代码:

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';
};

}); });

I want to got the other index from the current index, like if the current index is 2, then i want to get 0,1,3,4,5 since i have 6 index inside我想从当前索引中得到另一个索引,比如如果当前索引是 2,那么我想得到 0、1、3、4、5,因为我里面有 6 个索引

Through this , might you get your solution ,通过这个,你能不能得到你的解决方案,

In this JS script , you will not get the current index .在这个 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);
 }
}
});

Use Array.filter() to find the other indexes:使用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)

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

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