Alternative method for combining functions 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, unfortunately LIME does not support the splicing function.

Any idea how to create your own function to remove elements from an array?

Thank you for your time.

EDIT: Manage the creation of 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;
}
+5
source share
7 answers

Array.prototype.splice is fully defined in ECMA-262 §15.4.4.12, so use this as your specification and write. eg.

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

start, deleteCount () item1, item2 .., deleteCount item1, item2 .. Array, ( ) . :...

, , , , , , start + deleteCount .

, , , . , . , - Array Math.max/min. , .

push , .

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;
}
+4

, Array.prototype.splice dispires

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;
    }
}
+1

, , .

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

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

, .

+1

, , .

/**
 * Time Complexity: O(count) aka: O(1)
 */
function mySplice(array, start, count) {
  if (typeof count == 'undefined') count = 1

  while (count--) {
    var index2remove = start + count
    array[index2remove] = array.pop()
  }

  return array
}

, , :

/**
 * Time Complexity: O(count) aka: O(1)
 */
function mySplice(array, index, count) {
  if (typeof count == 'undefined') count = 1

  var removed = []

  while (count--) {
    var index2remove = index + count
    removed.push(array[index2remove])
    array[index2remove] = array.pop()
  }
  // for (var i = index; i < index + count; i++) {
  //    removed.push(array[i])
  //    array[i] = array.pop()
  // }

  return removed
}
+1

, LIME-?

, push() indexOf(), . , , . , splice(index, howMany, element1, elementN):

  • new
  • push() 0 index new
  • index push(), . LIME arguments, arguments > 2. .
  • , index + howMany , input.length

, , .

0

function splice()

array = mySplice(array,index,count);

- , mySplice()

 function mySplice(array, index, count)
{    
    var newArray = [];
    if( count > 0 ) 
        { count--;}
    else
        { count++;}
    for(i = 0; i <array.length; i++)
        {
            if(!((i <= index + count && i >=  index) || (i <= index && i >=  index + count)))  
            {
               newArray.push(array[i])
            }            
        }      
    return newArray;
}
0

,

function removeElements(a,index,n){
  // a=> Array , index=> index value from array to delete
 // n=> number of elements you want to delete
   let temp = []; // for storing deleted elements
   let main_array = []; // for remaining elements which are not deleted
   let k = 0;
   for(let i=0;i<a.length;i++){
      if((i===index) || ((index<i && i<n+index))){
      temp[i]=a[i+1];
      delete a[i];
       }
    if(a[i]!==undefined){
      main_array[k] = a[i]; 
      a[i] = main_array[k];
       k++;
       }
     }

      a=main_array;
   return a;
   }
  a=[1,2,3,4,5];
  console.log(removeElements(a,0,1));

Jsfiddle

0

All Articles