Giter VIP home page Giter VIP logo

Comments (4)

jwestbrook avatar jwestbrook commented on July 18, 2024

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

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

from prototype.

jwestbrook avatar jwestbrook commented on July 18, 2024

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]

from prototype.

jwestbrook avatar jwestbrook commented on July 18, 2024

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]

from prototype.

jwestbrook avatar jwestbrook commented on July 18, 2024

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

from prototype.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.