I have a array named sbjCodeArr
let sbjCodeArr = new Array();
And every time I click the button, data will push into sbjCodeArr
Ex)
- First time I click the button
['a']
- Second time I click the button
['a', 'b']
- Third time I click the button
['a', 'b', 'c']
But if, when I tried to push already exist element like 'a', then I want to prevent push 'a' again and alert message.
I tried like this code but it's not working. td.eq(0).text()
is a variable and every time I click the button it will hange and this value should be checked for a duplicate in the array (sbjCodeArr
).
let results=[];
for (var i = 0; i < sbjCodeArr.length; i++) {
if (sbjCodeArr[i + 1] == sbjCodeArr[i]) {
results.push(td.eq(0).text());
}
alert('repeated');
}
console.log(results);
How can I do that?