fastest way to detect if duplicate entry exists in javascript array?

前端 未结 8 2023
时光说笑
时光说笑 2021-02-06 09:54
var arr = [\'test0\',\'test2\',\'test0\'];

Like the above,there are two identical entries with value \"test0\",how to check it most efficiently?

8条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 10:51

        var index = myArray.indexOf(strElement);
        if (index < 0) {
            myArray.push(strElement);
            console.log("Added Into Array" + strElement);
        } else {
            console.log("Already Exists at " + index);
        }
    

提交回复
热议问题