Timeline for answer to How can I insert an item into an array at a specific index? by VisioN
Current License: CC BY-SA 3.0
Post Revisions
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 18, 2023 at 14:00 | comment | added | VisioN |
@TravisHeeter which makes sense, no? To iterate an array you need to use for (i of arr) {...}.
|
|
| Aug 31, 2023 at 19:39 | comment | added | Travis Heeter | Here's the direct quote from @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) {...} " | |
| Aug 29, 2023 at 19:02 | comment | added | VisioN | @TravisHeeter "loops won't work as expected" Why? How? Give an example. | |
| Aug 29, 2023 at 17:11 | comment | added | Travis Heeter | As stated above, using prototype will add an extra item to the array, so loops won't work as expected after. | |
| Jun 26, 2020 at 3:08 | comment | added | Cornchips007 | For some reason, when I go to use insert2, i get an "Expected 1 argument, but got 2" exception. | |
| Dec 12, 2014 at 8:46 | comment | added | VisioN |
@AlexisWilke In the first example I used slice method and not splice, which you're referring to in the comment. Second parameter of slice (named end) is zero-based index at which to end extraction. slice extracts up to but not including end. Hence after insert you have ["a", "b", "X", "Y", "Z", "c", "d"], from which slice extracts elements with indices from 1 up to 6, i.e. from "b" to "d" but not including "d". Does it make sense?
|
|
| Dec 12, 2014 at 2:08 | comment | added | Alexis Wilke |
Actually, if I use an index of 3 or more, I get nothing in the output (case 1., FireFox) ["a", "b", "c", "d"].insert(2, "X", "Y", "Z").slice(3, 3); => [ ]
|
|
| Dec 12, 2014 at 2:03 | comment | added | Alexis Wilke |
I don't understand the first result ["b", "X", "Y", "Z", "c"]. Why isn't "d" included? It seems to me that if you put 6 as the second parameter of slice() and there are 6 elements in the array starting from the specified index, then you should get all 6 elements in the return value. (The doc says howMany for that parameter.) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
|
|
| Apr 5, 2013 at 16:44 | history | edited | VisioN | CC BY-SA 3.0 |
added 609 characters in body
|
| Mar 30, 2013 at 23:56 | comment | added | Nolo | Is there a compact way to have this version also merge an array when it finds one in the arguments? | |
| Mar 25, 2013 at 17:45 | history | answered | VisioN | CC BY-SA 3.0 |