Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate method to splice function in JavaScript

Hi i am working on LIME programming which is a subset of javascript.

i need to use javascript.splice to remove certain elements from my array, sad to say, LIME does not support splice function.

Any idea how do i create my own function to remove elements from an array?

Thanks for your time.

EDIT: Manage to create a simple function.

function removeElements(array, index)
{
    var tempArray = new Array();
    var counter = 0;

    for(var i = 0; i < array.length; i++)
    {
        if(i != index)
        {
            tempArray[counter] = array[i];
            counter++;
        }
    }
    return tempArray;
}
like image 860
Dayzza Avatar asked Jun 29 '11 03:06

Dayzza


3 Answers

Array.prototype.splice is fully defined in ECMA-262 §15.4.4.12, so use that as your spec and write one. e.g.

15.4.4.12 Array.prototype.splice (start, deleteCount [ , item1 [ ,item2 [ , … ] ] ] )

When the splice method is called with two or more arguments start, deleteCount and (optionally) item1, item2, etc., the deleteCount elements of the array starting at array index start are replaced by the arguments item1, item2, etc. An Array object containing the deleted elements (if any) is returned. The following steps are taken:...

You will probably have to create a new array, copy the members up to start from the old array, insert the new members, then copy from start + deleteCount to the end to the new array.

Edit

Here is an amended splice, the first I posted was incorrect. This one splices the array passed in and returns the removed members. It looks a bit long but I tried to keep it close to the spec and not assume support for any complex Array methods or even Math.max/min. It can be simplified quite a bit if they are.

If push isn't supported, it can be replaced fairly simply too.

function arraySplice(array, start, deleteCount) {
  var result = [];
  var removed = [];
  var argsLen = arguments.length;
  var arrLen = array.length;
  var i, k;

  // Follow spec more or less
  start = parseInt(start, 10);
  deleteCount = parseInt(deleteCount, 10);

  // Deal with negative start per spec
  // Don't assume support for Math.min/max
  if (start < 0) {
    start = arrLen + start;
    start = (start > 0)? start : 0;
  } else {
    start = (start < arrLen)? start : arrLen;
  }

  // Deal with deleteCount per spec
  if (deleteCount < 0) deleteCount = 0;

  if (deleteCount > (arrLen - start)) {
    deleteCount = arrLen - start;
  }

  // Copy members up to start
  for (i = 0; i < start; i++) {
    result[i] = array[i];
  }

  // Add new elements supplied as args
  for (i = 3; i < argsLen; i++) {
    result.push(arguments[i]);
  }

  // Copy removed items to removed array
  for (i = start; i < start + deleteCount; i++) {
    removed.push(array[i]);
  }

  // Add those after start + deleteCount
  for (i = start + (deleteCount || 0); i < arrLen; i++) {
    result.push(array[i]);
  }

  // Update original array
  array.length = 0;
  i = result.length;
  while (i--) {
    array[i] = result[i];
  }

  // Return array of removed elements
  return removed;
}
like image 188
RobG Avatar answered Sep 21 '22 12:09

RobG


This modifies the original Array, and returns the items that were removed, just like the original.

Array.prototype.newSplice = function( start, toRemove, insert ) {
    var remove = this.slice( start, start + toRemove );
    var temp = this.slice(0,start).concat( insert, this.slice( start + toRemove ) );
    this.length = 0;
    this.push.apply( this, temp );
    return remove;
};

Comparison test: http://jsfiddle.net/wxGDd/

var arr = [0,1,2,3,4,5,6,7,8];
var arr2 = [0,1,2,3,4,5,6,7,8];


console.log( arr.splice( 3, 2, 6 ) );      // [3, 4]
console.log( arr );      // [0, 1, 2, 6, 5, 6, 7, 8]

console.log( arr2.newSplice( 3, 2, 6 ) );  // [3, 4]
console.log( arr2 );     // [0, 1, 2, 6, 5, 6, 7, 8]

It could use a little extra detail work, but for the most part it takes care of it.

like image 39
user113716 Avatar answered Sep 22 '22 12:09

user113716


Here is a simple implement in case the Array.prototype.splice dispears

if (typeof Array.prototype.splice === 'undefined') {
    Array.prototype.splice = function (index, howmany, elemes) {
        howmany = typeof howmany === 'undefined' || this.length;
        var elems = Array.prototype.slice.call(arguments, 2), newArr = this.slice(0, index), last = this.slice(index + howmany);
        newArr =  newArr.concat.apply(newArr, elems);
        newArr =  newArr.concat.apply(newArr, last);
        return newArr;
    }
}
like image 20
viclm Avatar answered Sep 20 '22 12:09

viclm