Archive Pages Design$type=blogging

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...

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.

<script>
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

تعليقات

الاسم

Artificial Intelligence(AI) Backlinking Basic IT Best List Blogging Tips C C# C++ Computer Architecture Computer Fundamental Computer Security Computer/IT Officer Exam CSS Data Mining and Warehousing Data Recovery Tools Data Structure Database Management System E-commerce E-government Internet & Web Designing IT Law IT Tips and Tricks IT Tutorials Java JavaScript Keyword Research Tools MIS Multiple Choice Question (MCQ) Networking Online Earning Online IT Jobs Operating System Oracle Forms and Reports Programming Guide Programming Language SEO Social Networking Sites Software Download Software Engineering System Analysis and Design Top List VB.Net
false
rtl
item
Max5: How to Concatenate, Join and Sort Array in JavaScript?
How to Concatenate, Join and Sort Array in JavaScript?
Max5
https://maxs27.blogspot.com/2015/02/how-to-concatenate-join-and-sort-array.html
https://maxs27.blogspot.com/
http://maxs27.blogspot.com/
http://maxs27.blogspot.com/2015/02/how-to-concatenate-join-and-sort-array.html
true
1760660989564394152
UTF-8
لم يتم العثور على اى مواضيع شاهد الكل اقرأ المزيد رد احذف الرد حذف بواسطة الرئيسية صفحات مواضيع شاهد الكل نوصى لك التسميات الارشيف بحث لم يتم العثور على ماتبحث عنه عودة للرئيسية الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر just now 1 دقيقة مضت $$1$$ دقيقة مضت 1 hour ago $$1$$ ساعة مضت الامس $$1$$ يوم مضى $$1$$ اسبوع مضى اكثر من 5 اسابيع مضت