从多个数组中删除元素

时间:2014-12-07 11:56:23

标签: javascript

如果我有:

var arr = ['2.4.1', 2004001, 'YYY', 'ddd', 'd1'];

然后我可以移除值'2.4.1'

arr.splice(arr.indexOf('2.4.1'), 1);

它有效,但不适用于IE7 / 8。


如何为我做:



var str = '2.4.1';

var arr = [
['1.1.3', 1001003, 'XXX', 'aaa', 'a3'],
['1.1.1', 1001001, 'XXX', 'aaa', 'a1'],
['1.3.1', 1003001, 'XXX', 'ccc', 'c1'],
['2.4.2', 2004002, 'YYY', 'ddd', 'd2'],
['2.4.1', 2004001, 'YYY', 'ddd', 'd1'],
['1.2.2', 1002002, 'XXX', 'bbb', 'b2'],
['1.2.1', 1002001, 'XXX', 'bbb', 'b1'],
];


arr.splice(4, 1);
//         ^_____ How to receive this index on my value '2.4.1' ?
//   Help to make cross browser and without JQuery, please.




2 个答案:

答案 0 :(得分:1)

IE< 9不会'知道'Array.indexOf,所以你需要一个polyfill。你可以找到@MDN

您还可以使用Array.filter删除包含“2.4.1”的数组(请参阅代码段):

arr = arr.filter( function (v) { return v[0] !== '2.4.1'; } );

对于IE< 9你需要this polyfill

// check for presence of Array.filter and if necessary add it
polyfills();

// declarations
var str = '2.4.1';
var arr = [
  ['1.1.3', 1001003, 'XXX', 'aaa', 'a3'],
  ['1.1.1', 1001001, 'XXX', 'aaa', 'a1'],
  ['1.3.1', 1003001, 'XXX', 'ccc', 'c1'],
  ['2.4.2', 2004002, 'YYY', 'ddd', 'd2'],
  ['2.4.1', 2004001, 'YYY', 'ddd', 'd1'],
  ['1.2.2', 1002002, 'XXX', 'bbb', 'b2'],
  ['1.2.1', 1002001, 'XXX', 'bbb', 'b1'],
];
var result = document.querySelector('#result');

// show original  
result.innerHTML = '<code>arr</code> initially:<br>' +
                   arr.map( function (v) { return v.join(); } ).join('<br>');

// remove using filter
arr = removesub(arr, str);
  
// show array after removal
result.innerHTML += '<hr><code>arr</code> after removing:<br>'+
                     arr.map( function (v) { return v.join(); } ).join('<br>');

  // removal function
function removesub(arr2RemoveFrom, str2Remove) {
  arr2RemoveFrom = arr2RemoveFrom.filter( 
                    function (v) { return v[0] !== str2Remove; } 
                   );
  return arr2RemoveFrom;
}
  

function polyfills() {
  // source MDN. Removed.comments
  if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun/*, thisArg*/) {
    'use strict';

    if (this === void 0 || this === null) {
      throw new TypeError();
    }

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== 'function') {
      throw new TypeError();
    }

    var res = [];
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++) {
      if (i in t) {
        var val = t[i];
        if (fun.call(thisArg, val, i, t)) {
          res.push(val);
        }
      }
    }

    return res;
  };
 }
  
 if (!Array.prototype.map) {

  Array.prototype.map = function(callback, thisArg) {

    var T, A, k;

    if (this == null) {
      throw new TypeError(' this is null or not defined');
    }

    var O = Object(this);
    var len = O.length >>> 0;
 
    if (typeof callback !== 'function') {
      throw new TypeError(callback + ' is not a function');
    }

    if (arguments.length > 1) {
      T = thisArg;
    }

    A = new Array(len);
    k = 0;

    while (k < len) {

      var kValue, mappedValue;
      if (k in O) {
        kValue = O[k];
        mappedValue = callback.call(T, kValue, k, O);
        A[k] = mappedValue;
      }
      k++;
    }
    return A;
  };
 }
}
code {
  color: red;
}
<div id="result"></div>

答案 1 :(得分:1)

您可以使用第一个值作为键,使用数组散列而不是数组数组,并使用方法delete

这样的事情:

var str = '2.4.1';

var arr = {
  '1.1.3': ['1.1.3', 1001003, 'XXX', 'aaa', 'a3'],
  '1.1.1': ['1.1.1', 1001001, 'XXX', 'aaa', 'a1'],
  '1.3.1': ['1.3.1', 1003001, 'XXX', 'ccc', 'c1'],
  '2.4.2': ['2.4.2', 2004002, 'YYY', 'ddd', 'd2'],
  '2.4.1': ['2.4.1', 2004001, 'YYY', 'ddd', 'd1'],
  '1.2.2': ['1.2.2', 1002002, 'XXX', 'bbb', 'b2'],
  '1.2.1': ['1.2.1', 1002001, 'XXX', 'bbb', 'b1']
};


delete arr[str];