00:00
00:00
B1KMusic
Hi. I make games and software. I'm not on NG much, but feel free to peruse my youtube and gitlab for interesting stuff.

Braden @B1KMusic

Age 29, Male

Programmer/Musician/

fish

Earth, Milky Way

Joined on 6/19/11

Level:
10
Exp Points:
948 / 1,110
Exp Rank:
69,525
Vote Power:
5.20 votes
Rank:
Police Sergeant
Global Rank:
8,626
Blams:
42
Saves:
1,040
B/P Bonus:
12%
Whistle:
Normal
Medals:
799

How to skip the [0] in an array

Posted by B1KMusic - February 3rd, 2012


say you're doing javascript or whateves, and you really don't feel like putting arrayshit[3] to get the fourth part of it, here is a quick way to skip the hassle using a second variable incase your ultra uber ZOMG EPIC ADVANCED PAGE!!!1 demands it:

var ar1 = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
var ar2 = [0]
for(i in ar1){ // Don't Use this with JsFiddle, the fucker glitches out. Use the longer for loop (for i=0;i<object.length;i++)

ar2[i+1] = ar1[i]

}
ar1 = ar2

this actually makes the array one step ahead of the other, meaning when you type 'alert(ar2[4])', it'll come back with "Fourth" instead of "Fifth". Do not, however, use the same variable (ar1[i+1] = ar1[i]), because common sense: it'll just put the var as [0, 'second', 'second', 'second', etc].

Why not just put the zero in the original array to begin with?

If you even have to ask that, make something advanced, like a snake game, or something where you really have no control of what the array is right away. Like taking information dynamically from user input. You can't expect them to put " 0,hello,hi,hello " instead of " hello,hi,hello " in a text field just so you can conveniently place ar[1] to get the first element, no user will do that, they will get annoyed that you couldn't figure out how to NOT make them do that, and leave.


Comments

Comments ain't a thing here.