When working with arrays in Javascript the way to insert the elements within an array is surely one of the simplest command, simply referencing the position and inhere information as shown in the example below:

<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
var vet = new Array ();
var i, next;
for (i = 0; i <= 5; i++) {

next = vet.length;
vet[next] = “Position ” + i;

}
for (i=0; i<vet.length; i++) {
document.write(vet[i] + “<BR>”);
}
</SCRIPT>

In the example above notice that we will go through the entire array by entering the information about the position of the array as the content of a particular index. At the end of the code will print to the screen the information about the positions of the Array, noting that the array always starts from position 0 (zero).

PUSH

The form above is one of the most common when you need to enter information or scan the array, but the object itself provides a method that helps to insert the data at the end of the index, the method PUSH. Using it you need not worry about how many positions the array has to insert, just use the method as a parameter passing elements to be added and the method automatically adds the elements at the end of the array, in addition, the number of lines of code also significantly decreases leaving the processing much faster. Check out the example below:

<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
var vet = new Array (“Item 1″);
document.write (“This arrays has “+vet.length+” item.<BR>”);
vetor.push(“Item 2″);
document.write (“Now, our array has “+vet.length+” items.<BR>”);
vetor.push(“Item 3″, “Item 4″, “Item 5″, “Item 6″);
document.write (“Now this array has “+vet.length+” items.<BR>”+
“Items Array: “+vetor.join(“, “));
</SCRIPT>

Note that each element is only added to the end of the queue and that its initial order does not change. Later they can be accessed from their certain indexes.

UNSHIFT

As the PUSH method, the unshift method is also used to insert data into an array, but it has some compatibility issues with Internet Explorer. So the most suitable method is to use the PUSH, check below for an example of using the unshift method:

<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
var vet = new Array (“Item 1″);
document.write (“This arrays has “+vet.length+” item.<BR>”);
vetor.unshift(“Item 2″);
document.write (“Now, our array has “+vet.length+” items.<BR>”);
vetor.unshift(“Item 3″, “Item 4″, “Item 5″, “Item 6″);
document.write (“Now this array has “+vet.length+” items.<BR>”+
“Items Array: “+vetor.join(“, “));
</SCRIPT>

As previously mentioned it can be used just like the PUSH method.

javascript

Understanding the resources that Javascript provides is much simpler to program and troubleshoot day to day. Often understanding the operation of certain functions and features complex problems can be solved in a few minutes.

Want to test this and other code in Javascript? Then download the free version already Scriptcase Moreover, you can also learn more about our tools and resources it offers everything to make your everyday life much easier. Stop wasting time, download now and discover a world of new possibilities.

You might also like…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.