0

If I have:

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

Then I can remove on value '2.4.1':

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

It works, but not in IE7/8.


How make for my:

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.

4
  • Does this help: stackoverflow.com/questions/3629183/… Commented Dec 7, 2014 at 12:01
  • If you're not keen on polyfill, it's more code but you can fetch, identify the correct key with a regexp, and then delete it. Commented Dec 7, 2014 at 12:04
  • When you say "remove" you mean "delete" a value from the array or just copy it? Commented Dec 7, 2014 at 12:07
  • "remove" -> delete key and value Commented Dec 7, 2014 at 12:09

2 Answers 2

1

IE < 9 doesn't 'know' Array.indexOf, so you'll need a polyfill for it. You can find that @MDN

You can also use Array.filter to remove the array containing '2.4.1' (see snippet):

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

For IE < 9 you need 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>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer. I looked 'polyfill' earlier but could not fasten to my case.
Hi @Alex, see modified answer for an alternative
1

You can use the first value as a key, using a hash of arrays instead of a array of arrays and use the method delete.

Something like this:

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];

1 Comment

After remove there are inserts elements and array sorting (by 1001001 ... ) So I can not to make. Thanks. Good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.