Showing posts with label array. Show all posts
Showing posts with label array. Show all posts
Thursday, May 25, 2017
How to Concatenate Join and Sort Array in JavaScript
How to Concatenate Join and Sort Array in JavaScript
In the previous post, I have already described about "How to Loop Through JavaScript Array". Along with accessing an element of an array using looping statements, you can also concatenate, join and sort an array in JavaScript. In this post you will learn about How to Concatenate, Join and Sort Array in JavaScript.
How to Join Two Arrays using Concat()
Using concat() method, you can join to arrays array1 and array2 as below.
var array1=new Array(3);
array1[0]="Saab"
array1[1]="Volvo"
array1[2]="BMW"
var array2=new Array(3);
array2[0]="Yamaha"
array2[1]="Honda"
array2[2]="Bajaj"
var array3=array1.concat(array2);
for(i=0; i<array3.length;i++)
{
document.write(array3[i]+"<br/>")
}
</script>
Preview:
Here the method array1.concat(array2) concentrates two arrays array1 and array2.
How to Put Array Elements Into a String using Join()
You can use the join() method to put all the elements of an array into a string as the following script.
<script>
var array1=new Array(3);
array1[0]="Yamaha"
array1[1]="Honda"
array1[2]="Bajaj"
document.write(array1.join()+"<br/>");
document.write (array1.join("."));
</script>
Preview:
How to Sort String Array using sort()
Using sort() method, you can sort the elements of string array as given below.
<script>
var array1=new Array(6);
array1[0]="Saab"
array1[1]="Volvo"
array1[2]="BMW"
array1[3]="Yamaha"
array1[4]="Honda"
array1[5]="Bajaj"
document.write(array1+"<br/>");
document.write (array1.sort());
</script>
Preview:
How to Sort Numeric Array using sort()
Using sort() method, you can sort the elements of numeric array as given below.
<script>
var array2=new Array(6);
array2[0]=10
array2[1]=5
array2[2]=40
array2[3]=25
array2[4]=35
array2[5]=95
function sortNumber(a,b) {return a - b;}
document.write(array2+"<br/>");
document.write (array2.sort(sortNumber));
</script>
Preview:
There are also other more methods for manipulating array elements, which are listed below along with their description. You can use them as the methods given above.
- pop(): Removes and returns the last element of an array
- push(): Adds one or more elements to the end of an array and returns the new length.
- reverse(): Reverses the order of the elements in an array
- toString(): Converts an array to string and returns the result.
Read Next:How to use Round, Random, Min and Max in JavaSript
Related Search Terms
Concate Array in JavaScript
Join Array in JavaScript
Sort Array in JavaScript
Related Posts:
How to Show Pop Up Boxes Using JavaScript?
How to Write Conditional Statements in JavaScript?
How to Write JavaScript With HTML?
How to Loop using JavaScript?
Image Slideshow with Navigation Buttons Using JavaScript
How to create Changeable Date and Time Using JavaScript?
How to Create JavaScript Image Slideshow with LInks
How to Display Date Format in JavaScript?
How to Validate a HTML Form Using JavaScript?
What are the Different Ways to Redirect Page in JavaScript?
How to Detect Visitors Browser Using JavaScript?
How to make rounded corners border using CSS
How to Create Custom CSS Template for Printing
How to create a simple form using HTML?
Go to link Download
Labels:
and,
array,
concatenate,
how,
in,
javascript,
join,
sort,
to
Sunday, May 7, 2017
How to Loop Through JavaScript Array
How to Loop Through JavaScript Array
You can loop any Array by using different types of looping statements in JavaScript, which I have previously posted. In this post you can loop through JavaScript Array. At first let you know basic concepts of Array ie. defining array, adding values to the array and accessing array elements.
The Array object is used to store a set of values in a single variable name. You can define Array object with the new keyword.
The following line of code defines an Array object called myArray.
var myArray=new Array()
You can also define the array size by passing an integer argument as follows.
var myArray=new Array(5)
You can add the values to an array as the following.
var myArray=new Array()
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"
You can also give values to the arrays while defining it as below.
var myArray=new Array("value1", "value2", "value3")
While accessing Arrays, you can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0. The following line accesses the first value of the array myArray.
document.write(myArray[0])
You can loop any Array by using any of the following looping statements in JavaScript.
You can loop any Array by using For Loop as below.
var myArray=new Array()
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"
for(i=0; i<myArray.length;i++)
{
document.write(myArray[i])
}
<html>
<head></head>
<body>
<script type="text/javascript">
var mycars=new Array()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"
for(i=0; i<mycars.length;i++)
{
document.write(mycars[i]+"<br/>")
}
</script>
The Array object is used to store a set of values in a single variable name. You can define Array object with the new keyword.
The following line of code defines an Array object called myArray.
var myArray=new Array()
You can also define the array size by passing an integer argument as follows.
var myArray=new Array(5)
You can add the values to an array as the following.
var myArray=new Array()
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"
You can also give values to the arrays while defining it as below.
var myArray=new Array("value1", "value2", "value3")
While accessing Arrays, you can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0. The following line accesses the first value of the array myArray.
document.write(myArray[0])
Loop Through JavaScript Array
You can loop any Array by using any of the following looping statements in JavaScript.
Using For loop
You can loop any Array by using For Loop as below.
var myArray=new Array()
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"
for(i=0; i<myArray.length;i++)
{
document.write(myArray[i])
}
Example:
<html>
<head></head>
<body>
<script type="text/javascript">
var mycars=new Array()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"
for(i=0; i<mycars.length;i++)
{
document.write(mycars[i]+"<br/>")
}
</script>
Preview:
Using For .... In Statement
You can loop any Array by using For .... In Statement as below.
var myArray=new Array()
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"
for(x in myArray)
{
document.write(myArray[x])
}
Example:
<html>
<head></head>
<body>
<script type="text/javascript">
var mycars=new Array()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"
for(x in mycars)
{
document.write(mycars[x]+"<br/>")
}
</script>
</body>
</html>
Preview:
You can also loop any Array by using other looping statements like while and Do ... while loops as the same methods given above.
Read Next:How to Concatenate, Join and Sort Array in JavaScript?
Related Search Terms
Javascript For Loop Through Array
Javascript Array For .. In Statement
JavaScript Array Loop
Related Posts:
How to Show Pop Up Boxes Using JavaScript?
How to Write Conditional Statements in JavaScript?
How to Write JavaScript With HTML?
How to Loop using JavaScript?
Image Slideshow with Navigation Buttons Using JavaScript
How to create Changeable Date and Time Using JavaScript?
How to Create JavaScript Image Slideshow with LInks
How to Display Date Format in JavaScript?
How to Validate a HTML Form Using JavaScript?
What are the Different Ways to Redirect Page in JavaScript?
How to Detect Visitors Browser Using JavaScript?
How to make rounded corners border using CSS
How to Create Custom CSS Template for Printing
How to create a simple form using HTML?
Go to link Download
Subscribe to:
Posts (Atom)