2013-04-30 6 views
11

Sto cercando di creare una funzione JavaScript che cercherà una serie di stringhe per un valore e restituirà la stringa successiva. Ad esempio, se una matrice è costruita in modo tale che un articolo sia seguito dal suo codice azionario, voglio cercare l'articolo e far scrivere il codice azionario.Javascript: generico ottiene l'elemento successivo nell'array

var item = (from user input); //some code to get the initial item from user 
function findcode(code){ 
    var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array 
    for (var i=0; i<arr.lenth; i++){ //for loop to look through array 
    arr.indexOf(item); //search array for whatever the user input was 
    var code = arr(i+1); //make the variable 'code' whatever comes next 
    break; 
    } 
} 
document.write(code); //write the code, I.e., whatever comes after the item 

(io sono sicuro che è ovvio Sono nuovo di JavaScript, e mentre questo è simile a una serie di altre questioni che ho trovato, chi sembrava avere le matrici più coinvolti o ricerche più complesse. Posso' t sembrano semplificarle per le mie esigenze)

risposta

38

Hai quasi capito bene, ma la sintassi è arr[x], non arr(x):.

index = array.indexOf(value); 
if(index >= 0 && index < array.length - 1) 
    nextItem = array[index + 1] 

BTW, utilizzando un oggetto invece di un array potrebbe essere un opzione migliore:

data = {"ball":"1f7g", "spoon":"2c8d", "pen":"9c3c"} 

e poi semplicemente

code = data[name] 
+2

+1 Solo per aggiungere, ['Array.indexOf' è ES5] (https : //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf) e non funzionerà con i browser più vecchi. – Joseph

+0

@ thg435 Grazie mille! Questo è esattamente ciò di cui avevo bisogno. E grazie anche per l'alternativa dell'oggetto. – jak

+0

Semplice e utile, Ovviamente le condizioni per l'articolo precedente devono essere (indice> 0 && indice QMaster

3

Penso che un oggetto potrebbe essere probabilmente una migliore struttura di dati per questo tipo di compito

items = { 
    ball : "1f7g", 
    spoon: "2c8d", 
    pen : "9c3c" 
} 


console.log(items['ball']); // 1f7g 
+0

+1 mi chiedo se l'OP sapesse che questo comportamento è hash/array associativo? – booyaa

+0

@booyaa Non l'ho fatto, ma ci sto leggendo ora. Grazie :) – jak

+0

@jak potresti anche trovare utile sapere su JSON! – booyaa

0

Si può passare la matrice di funzionare come argomento e ritorno valore trovato dalla funzione:

var item = "spoon"; // from user input 
var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array 
function findcode(item, arr){ 
    var idx = arr.indexOf(item); //search array for whatever the user input was 
    if(idx >=0 && idx <= arr.length - 2) { // check index is in array bounds 
     return arr[i+1]; // return whatever comes next to item 
    } 
    return ''; 
} 
document.write(findcode(item, arr)); //write the code, i.e., whatever comes after the item 
Problemi correlati