JavaScript array merging and sorting examples
Merge two Arrays-concat ()
Source code:
<! DOCTYPE html> <body> <p id = "demo"> click the button to merge arrays. </P> <button onclick = "myFunction ()"> click me </button> <script> function myFunction () {var hege = ["Cecilie ", "Lone"]; var stale = ["Emil", "Tobias", "Linus"]; var children = hege. concat (stale); var x = document. getElementById ("demo"); x. innerHTML = children ;}</script> </body>
Test results:
Cecilie,Lone,Emil,Tobias,Linus
Merge three Arrays-concat ()
Source code:
<!DOCTYPE html>
Test results:
Jani,Tove,Stale,Kai Jim,Borge,Cecilie,Lone
Array sorting (alphabetically ascending)-sort ()
Source code:
<!DOCTYPE html>
Test results:
Apple,Banana,Mango,Orange
Sort numbers (ascending order of numbers)-sort ()
Source code:
<!DOCTYPE html>
Test results:
1,5,10,25,40,100
Sort numbers (in descending order)-sort ()
Source code:
<!DOCTYPE html>
Test results:
100,40,25,10,5,1
Sort the elements in an array in reverse order-reverse ()
Source code:
<!DOCTYPE html>
Test results:
Mango,Apple,Orange,Banana