How do I specify multiple index values of an array without iterating through all of them?
I have an array: var boxArray = $(’.box’).toArray();. I have 9 elements with the class box, so this array is comprised of the index values [0, 1, 2, 3, 4, 5, 6, 7, 8].
If boxArray index values of [0], [4], and [8] are all given a class of clicked, I want something to happen. I know I can use a for loop to iterate through the entire array, and I know I can reference just one index value (boxArray[0]), but how would I denote multiple out-of-order index values at once?
$(boxArray[0, 4, 8]) doesn’t work - only the last index value is recognized. $(boxArray[0][4][8]) doesn’t work
Is the only way to do this in JavaScript and jQuery to reference them one at a time? Ideally I'd like to be able to do something like this:
if ($(boxArray[0, 4, 8]).hasClass(‘clicked’)) {
// do something;
}
instead of
if ($(boxArray[0]).hasClass('clicked') &&
$(boxArray[4]).hasClass('clicked') &&
$(boxArray[8]).hasClass('clicked')
) {
// do something
}
0,4and8? What do they have in commun? Can they be selected by something?