How do I define the index within the array?

Add to variable index ++ (var[i++]="first from array";) with javascript?

  • example: for (i in myArray){ mynew[n++]=myArray; } How to increment the index automaticaly in a loop. If I do myarray=new Array(); and latter myarray="first"; next time myarray="content"; will not result an array.

  • Answer:

    Do not use a for in loop to iterate over the elements of an array. Use: for (var i = 0; i < myArray.length; i++) {     myArray[i] = //Whatever you're trying to do. } Use the array literal notation instead of new. Example: var myArray = []; Instead of: var myArray = new Array(); If you assign myArray as an array type, then reassign it as a string, of course the result will be a string instead of an array. If you want to assign a string to an array element then use an index. Example: myArray[0] = "first"; //Assigns the string "first" to the element at index 0 of the array. - Dominic

Hooks at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.