Comments (4)
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.
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.
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.
[@]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)
- wrap function with multiple arguments where some arguments are optional
- Object.isNumber returns true on NaN HOT 1
- Native Array.prototype.filter is overwritten by prototype HOT 1
- Native Array.from is overwritten by $A which is not able to handle all cases used by the native function HOT 5
- Unable to build on Windows (no tar command) HOT 5
- Uncaught TypeError: $(...).getValue is not a function HOT 5
- Global.Node has the potential of being overwritten.
- Array.prototype.toJSON requires double parsing after stringify HOT 1
- Compatibility with RetroZilla HOT 6
- Security Issue: Request for contact HOT 4
- Uncaught SyntaxError: unexpected token: ':' HOT 2
- https://api.prototypejs.org/ seems to be down. HOT 3
- Warning: Potential Security Risk Ahead https://api.prototypejs.org/ HOT 1
- Fixing JS injection HOT 2
- Typescript declarations?
- CVE-2020-27511 HOT 5
- Element.replace() swaps element and textNode if used inside a table
- prototype.js is causing chart.js to not work HOT 2
- Array.from doesn't work with some iterables HOT 1
- $F throws an error when missing element
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prototype.