#356 enhancement
quamis

Add Array#insert

Reported by quamis | September 24th, 2008 @ 09:28 AM

I needed to b able to insert values in the middle of an array and did not find this functionality in prototype..so here it is:)

//extend the array object to support indexed insertions Array.prototype.insert=function(element,where) {

var slice1=this.slice(0,where);
var slice2=this.slice(where);

return new Array.concat(slice1,element,slice2);

};

Comments and changes to this ticket

  • Tobie Langel

    Tobie Langel September 24th, 2008 @ 11:18 AM

    • State changed from “new” to “invalid”

    That's already possible with the native splice method.

  • Joe Gornick

    Joe Gornick September 24th, 2008 @ 07:15 PM

    Here's a helper method I use to insert values into an array:

    
    Object.extend(Array.prototype, {
      insert: function(index) {
        this.length = (index > this.length) ? index : this.length;
        index = (index < 0) ? this.length : index;
    
        this.splice.apply(this, [index, 0].concat(Array.slice(arguments, 1)));
        return this;
      }
    });
    

    Examples:

    
    var a1 = new Array(1, 2, 3, 4, 5);
    a1.insert(0, 0); // -> [0, 1, 2, 3, 4, 5]
    a1.insert(-1, 6); // -> [0, 1, 2, 3, 4, 5, 6]
    a1.insert(10, 10); // -> [0, 1, 2, 3, 4, 5, 6, undefined, undefined, undefined, 10]
    
  • Joe Gornick

    Joe Gornick September 24th, 2008 @ 07:18 PM

    Forgot to mention the insert method can take multiple arguments to insert.

    To continue with the above example:

    
    a1.insert(11, 11, 12, 13, 14); // -> [0, 1, 2, 3, 4, 5, 6, undefined, undefined, undefined, 10, 11, 12, 13, 14]
    
  • John-David Dalton

    John-David Dalton September 24th, 2008 @ 07:48 PM

    @Joe - Very cool stuff :D.

    Here are a couple tweaks:

    Array.slice is not spec complient and browsers such as IE don't implement the method on the Array constructor.

    We should use Array.prototype.slice.call

    Also (index > this.length) ? index : this.length; could be Math.max(this.length, index)

    
      insert: function(index) {
        this.length = Math.max(this.length, index);
        index = index < 0 ? this.length : index;
    
        this.splice.apply(this, [index, 0]
          .concat(Array.prototype.slice.call(arguments, 1)));
        return this;
      }
    

    Possible performance tweak (probably overkill here)

    
      insert: function(index) {
        var args = Array.prototype.slice.call(arguments, 1);
        this.length = Math.max(this.length, index);
        index = index < 0 ? this.length : index;
    
        if (args.length > 1)
          this.splice.apply(this, [index, 0].concat(args));
        else this.splice(index, 0, args[0]);
        return this;
      }
    
  • John-David Dalton

    John-David Dalton September 24th, 2008 @ 08:03 PM

    • Title changed from “Array.insert” to “Add Array#insert”
    • State changed from “invalid” to “enhancement”
  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    • Tag cleared.

    [responsible:none bulk edit command]

  • Tisho Georgiev

    Tisho Georgiev March 1st, 2010 @ 09:54 PM

    • Tag set to needs:tests, section:lang
  • Jason Westbrook

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Pages