2013-06-25 18 views
1

約翰Resig的刪除索引的數組,jQuery的作者創建a very handy Array.remove的方法,我一直用它在我的項目:從陣列

// Array Remove - By John Resig (MIT Licensed) 
Array.prototype.remove = function(from, to) { 
    var rest = this.slice((to || from) + 1 || this.length); 
    this.length = from < 0 ? this.length + from : from; 
    return this.push.apply(this, rest); 
}; 

// Remove the second item from the array 
array.remove(1); 
// Remove the second-to-last item from the array 
array.remove(-2); 
// Remove the second and third items from the array 
array.remove(1,2); 
// Remove the last and second-to-last items from the array 
array.remove(-2,-1); 

它的偉大工程。但我想知道它是否可擴展,以便它可以將一組索引作爲第一個參數?

否則,我可能會做出這使得使用它的另一種方法:

if (!Array.prototype.removeIndexes) { 

    Array.prototype.removeIndexes = function (indexes) { 

     var arr = this; 

     if (!jQuery) 
      throw new ReferenceError('jQuery not loaded'); 

     $.each(indexes, function (k, v) { 

      var index = $.inArray(v, indexes); 

      if (index !== -1) 
       arr.remove(index); 

     }); 
    }; 
} 

如果Array.remove()沒有擴展到適合我的需要,有什麼你覺得我上面的其他解決方案?

+2

http://codereview.stackexchange.com/吧? – techfoobar

+0

不要修改你不屬於自己的對象 - 即使J. Resig這樣做 – tmaximini

+0

@techfoobar謝謝,不知道那個網站。 – Johan

回答

1

這個版本應該工作。它修改了原始數組。如果您希望在不修改原始數組的情況下返回一個新數組,請使用result的註釋初始化程序,並在該函數的末尾添加return result

Array.prototype.removeIndexes = function(indices) { 
    // make sure to remove the largest index first 
    indices = indices.sort(function(l, r) { return r - l; }); 

    // copy the original so it is not changed 
    // var result = Array.prototype.slice.call(this); 

    // modify the original array 
    var result = this; 

    $.each(indices, function(k, ix) { 
    result.splice(ix, 1); 
    }); 
} 

> [0, 1, 2, 3, 4, 5, 6, 7, 8].removeIndexes([4, 5, 1]); 
> [0, 2, 3, 6, 7, 8] 
+0

http://jsfiddle.net/N8T5Q/我錯過了什麼嗎? – Johan

+0

@Johan:這個函數在不修改原始的情況下返回一個新的數組。 – DCoder

+0

好吧,它可以調整,以便它修改您使用它的實際數組? – Johan

0

如何

Array.prototype.remove = function (indexes) { 
    if(indexes.prototype.constructor.name == "Array") { 
     // your code to support indexes 
    } else { 
     // the regular code to remove single or multiple indexes 
    } 

}; 
2

我認爲這是你在找什麼(它的工作原理與負指數太):

if (!Array.prototype.removeIndexes) { 

    Array.prototype.removeIndexes = function (indexes) { 

     var arr = this; 

     if (!jQuery) throw new ReferenceError('jQuery not loaded'); 

     var offset = 0; 

     for (var i = 0; i < indexes.length - 1; i++) { 
      if (indexes[i] < 0) 
       indexes[i] = arr.length + indexes[i]; 
      if (indexes[i] < 0 || indexes[i] >= arr.length) 
       throw new Error('Index out of range'); 
     } 

     indexes = indexes.sort(); 

     for (var i = 0; i < indexes.length - 1; i++) { 
      if (indexes[i + 1] == indexes[i]) 
       throw new Error('Duplicated indexes'); 
     } 


     $.each(indexes, function (k, index) { 
      arr.splice(index - offset, 1); 
      offset++; 
     }); 
     return arr; 
    }; 
} 

var a = ['a', 'b', 'c', 'd', 'e', 'f']; 
var ind = [3, 2, 4]; 

a.removeIndexes(ind); 

console.log(a.join(', ')); 
// returns : a, b, f 

See fiddle

+0

謝謝。如果我傳遞一個不存在的索引,會發生什麼?像'-1' – Johan

+0

如果一個索引<0,不應該使用,對吧? – Johan

+1

如果你傳遞了一個錯誤的索引,就會拋出一個錯誤。 可以使用負數從陣列的端部選擇(如剪接功能)。 – Damien