Timeline for answer to How can I insert an item into an array at a specific index? by FrEsC 81
Current License: CC BY-SA 4.0
Post Revisions
20 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 7, 2023 at 16:39 | comment | added | bsplosion |
Modifying a built-in just to remove one parameter from an existing method for that same built-in is certainly an approach... you're literally saying either arr.splice(2, 0, 'C', 'D') or arr.insert(2, 'C', 'D'). Just use splice.
|
|
| Oct 3, 2022 at 18:38 | comment | added | Mr. Smit | it will implement bug in your code when other arrays may not work anymore | |
| S Aug 22, 2022 at 15:17 | history | suggested | AMJ | CC BY-SA 4.0 |
splice allows more, why shouldn't this
|
| Aug 22, 2022 at 4:26 | review | Suggested edits | |||
| S Aug 22, 2022 at 15:17 | |||||
| May 31, 2019 at 12:54 | comment | added | satya164 | Don't modify prototypes | |
| May 11, 2019 at 7:18 | comment | added | Ryan Smith | @SolomonUcko that is true, if you're working with large arrays that is something to consider. For smaller arrays, I think that the benefits of immutability outweigh the negligible performance implications. Note that there is an answer here that uses this approach stackoverflow.com/a/38181008/1221906 so probably best to provide feedback there for people that are using this approach. | |
| May 9, 2019 at 20:14 | comment | added | Solomon Ucko | @RyanSmith The problem is that that has to copy the start of the array, not just the end. | |
| May 9, 2019 at 20:13 | comment | added | Ryan Smith |
@SolomonUcko yeah you could tbh for ES6 I'd just do [...arr.slice(0, index), ...items, ...arr.slice(index)]
|
|
| May 5, 2019 at 2:39 | comment | added | Solomon Ucko |
@RyanSmith Or, with ES6, Array.prototype.insert = function(index, ...items) { this.splice.call(this, index, 0, ...items); }.
|
|
| Feb 19, 2019 at 18:09 | comment | added | Parzibyte | Don’t modify objects you don’t own | |
| Aug 21, 2018 at 9:48 | comment | added | marsze | But keep in mind that it's not recommended to extend native types since it might interfere with other code or future functionality. | |
| May 1, 2018 at 14:42 | review | Suggested edits | |||
| May 1, 2018 at 14:46 | |||||
| Mar 30, 2018 at 21:26 | history | edited | K.Dᴀᴠɪs | CC BY-SA 3.0 |
added 2 characters in body
|
| S Mar 9, 2017 at 11:11 | history | suggested | Sami | CC BY-SA 3.0 |
improved formatting
|
| Mar 9, 2017 at 10:09 | review | Suggested edits | |||
| S Mar 9, 2017 at 11:11 | |||||
| Jul 2, 2014 at 9:38 | comment | added | rep_movsd | The problem with adding stuff to array is that the function will show up as an element when you do for(i in arr) {...} | |
| May 30, 2014 at 12:15 | comment | added | Ryan Smith |
To insert multiple items you can use Array.prototype.insert = function (index, items) { this.splice.apply(this, [index, 0].concat(items)); }
|
|
| S Oct 3, 2012 at 14:28 | review | Late answers | |||
| Oct 3, 2012 at 17:49 | |||||
| S Oct 3, 2012 at 14:28 | review | First posts | |||
| Oct 3, 2012 at 15:06 | |||||
| Oct 3, 2012 at 14:26 | history | answered | FrEsC 81 | CC BY-SA 3.0 |