Skip to content Skip to sidebar Skip to footer

Javascript : How Can I Get Array In Different Ways?

hi i have a little problem with my javascript can i make the simple way to execute content of array with different character of word? for example : var word = new Array(); word [0]

Solution 1:

I'm not exactly sure what you are chasing (fill me in and I'll update), but I'd like to point out a far better way of filling in that array literal...

var word = [
   'is',
   'am'
];

You can see the index is calculated automatically, and you are not required to repeat the var name for each member definition.

Update

Maybe you want something you can call and get the next array member each time. This should do it...

functiongetNextMember(array, startIndex) {
   startIndex = startIndex || 0;

   returnfunction() {
      startIndex++;  
      returnarray[startIndex];
   };
   
};

var getNextWord = getNextMember(word);

alert(getNextWord() + ' ' + getNextWord());

See it on jsFiddle.

And of course, if you are feeling naughty, you could add that function to Array's prototype.

Post a Comment for "Javascript : How Can I Get Array In Different Ways?"