Adam
Adam

Reputation: 115

How to get a string's order number in an array in Javascript?

Here's an array:

var array = new Array();
array[0] = "sound1.mp3"
array[1] = "sound2.mp3"
array[2] = "sound3.mp3"

In a function that I've written, I need to get the string's order number.

Here's what I've got so far:

function registerSound(array, i) {
    var arrno = ???;
    var arrurl = array[i];

    soundManager.createSound({ id: 'sound' + arrno, url: arrurl });
}

function processArray(array) {;
    for(i=1; i<array.length; i++) {
         registerSound(array, i);
    }
}

The arrno variable should have the order number of "sound2.mp3" (in this case 1). It needs to be set up so that, when the for statement turns to the next string in the array, the arrno variable will contain the order number of "sound3.mp3" (2).

Thanks very much in advance!

Upvotes: 2

Views: 207

Answers (1)

Esailija
Esailija

Reputation: 140230

function registerSound(array, i) {
    var arrurl = array[i];
    soundManager.createSound({
        id: 'sound' + i,
        url: arrurl
    });
}

Also, declare your variables and start your loop from 0:

function processArray(array) {;
    for (var i = 0; i < array.length; i++) {
        registerSound(array, i);
    }
}

Upvotes: 1

Related Questions