Javascript Array inject(index, val)

Description

Javascript Array inject(index, val)


/**/*from ww  w.ja  va2  s  .  co  m*/

@Description: Finds the index of the value given within the array.  Return the position of the first matching value.  Rememeber that array start at 0.
@Param: Object/String/Number val The value to inject into the array. 
@Return: Integer
@Example:
var myArray = ['zero', 'one', 'two'];
var answer = myArray.inject(1, 'bagel');

//answer is now ['zero', 'bagel', 'one', 'two'];
*/
Array.prototype.inject = function(index, val){
 if(index <0){return this;}
 var a = this.slice(0, index), b = this.splice(index, this.length-index);
 
 a[index] = val;
 return a.concat(b);
};



PreviousNext

Related