2012-05-29 12 views
7

qualcuno può dirmi come posso rimuovere elemento stringa da una matrice ho google questo e tutto quello che ottiene è la rimozione dal numero di indicerimuovere elemento stringa da JavaScript gamma

il mio esempio:

var myarray = ["xyz" , "abc" , "def"] ; 
var removeMe = "abc" ; 

    myarray.remove(removeMe) ; 
    consle.log(myarray) ; 

questo è ciò che ottengo dalla console:

Uncaught TypeError: Object xyz,abc,def has no method 'remove' 

jsfiddle

risposta

6

Da https://stackoverflow.com/a/3955096/711129:

Array.prototype.remove= function(){ 
    var what, a= arguments, L= a.length, ax; 
    while(L && this.length){ 
     what= a[--L]; 
     while((ax= this.indexOf(what))!= -1){ 
      this.splice(ax, 1); 
     } 
    } 
    return this; 
} 
var ary = ['three', 'seven', 'eleven']; 

ary.remove('seven') 

o, il che rende una funzione globale:

function removeA(arr){ 
var what, a= arguments, L= a.length, ax; 
while(L> 1 && arr.length){ 
    what= a[--L]; 
    while((ax= arr.indexOf(what))!= -1){ 
     arr.splice(ax, 1); 
    } 
} 
return arr; 
} 
var ary= ['three','seven','eleven']; 
removeA(ary,'seven') 

Devi fare una funzione da soli. Puoi eseguire il looping sull'array e rimuovere l'elemento da lì, oppure farlo funzionare per te. Ad ogni modo, non è una funzionalità JS standard.

5

Prova come qui di seguito,

myarray.splice(myarray.indexOf(removeMe),1); 

È possibile aggiungere questo script di seguito (from MDN) per i browser che non supporta indexOf

if (!Array.prototype.indexOf) { 
    Array.prototype.indexOf = function (searchElement /*, fromIndex */) { 
     "use strict"; 
     if (this == null) { 
      throw new TypeError(); 
     } 
     var t = Object(this); 
     var len = t.length >>> 0; 
     if (len === 0) { 
      return -1; 
     } 
     var n = 0; 
     if (arguments.length > 0) { 
      n = Number(arguments[1]); 
      if (n != n) { // shortcut for verifying if it's NaN 
       n = 0; 
      } else if (n != 0 && n != Infinity && n != -Infinity) { 
       n = (n > 0 || -1) * Math.floor(Math.abs(n)); 
      } 
     } 
     if (n >= len) { 
      return -1; 
     } 
     var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); 
     for (; k < len; k++) { 
      if (k in t && t[k] === searchElement) { 
       return k; 
      } 
     } 
     return -1; 
    } 
} 
+4

Un commento, alcuni browser non supportano 'indexOf' – jwatts1980

+1

Inoltre, se' indexOf' non trova l'elemento, si è giuntura andando con l'indice '-1' che rimuove comunque un elemento – Esailija

16

Dal momento che si sta utilizzando jQuery

myarray.splice($.inArray("abc", myarray), 1);

MODIFICA Se l'elemento non è nell'array, questo "one-liner" genererà probabilmente un errore. Qualcosa di un po 'meglio

var index = $.inArray("abc", myarray); 
if (index>=0) myarray.splice(index, 1); 
+2

Non è necessario utilizzare jQuery per questo. –

+3

Alcuni browser non supportano 'indexOf'. 'InArray' di Jquery estrae la funzionalità dal browser. – jwatts1980

+0

+1 per l'utilizzo di '> = 0' http://jsperf.com/not-vs-gt-vs-ge/4 (e per la risposta corretta di c) – ajax333221

Problemi correlati