Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array#insert #149

Open
jwestbrook opened this issue Apr 22, 2014 · 4 comments
Open

Add Array#insert #149

jwestbrook opened this issue Apr 22, 2014 · 4 comments

Comments

@jwestbrook
Copy link
Collaborator

previous lighthouse ticket #356
by quamis


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

};
@jwestbrook
Copy link
Collaborator Author

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

State changed from “new” to “invalid”
That's already possible with the native splice method.

@jwestbrook
Copy link
Collaborator Author

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]

@jwestbrook
Copy link
Collaborator Author

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]

@jwestbrook
Copy link
Collaborator Author

[@]jdalton
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;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants