• We just launched and are currently in beta. Join us as we build and grow the community.

How to Copy an Array to Other Array in JavaScript

mathluscan

Cyber Risk Analyst
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
149
Likes
13
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Copy an Array to Other Array in JavaScript

Introduction

In this tutorial we will create a How to Copy an Array to Other Array in JavaScript. This tutorial purpose is to teach you how to copy an array to another array. This will tackle all the basic function that will copy the array. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that use array as a temporary data storage. I will give my best to provide you the easiest way of creating this program Calculate GPA automatically. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the tables and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodster</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Copy an Array to Other Array</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <form

    action

    =

    "javascript:void(0);"

    method

    =

    "POST"

    class

    =

    "form-group"

    onsubmit

    =

    "insert()"

    >
  19. <label

    >

    Enter here</

    label

    >
  20. <input

    type

    =

    "text"

    id

    =

    "name"

    class

    =

    "form-control"

    /

    >
  21. <br

    /

    >
  22. <button

    type

    =

    "submit"

    class

    =

    "btn btn-primary form-control"

    >

    Save</

    button

    >
  23. </

    form

    >
  24. </

    div

    >
  25. <div

    class

    =

    "col-md-3"

    >
  26. <table

    class

    =

    "table table-bordered"

    >
  27. <thead

    class

    =

    "alert-info"

    >
  28. <tr

    >
  29. <th

    >

    Array 1</

    th

    >
  30. </

    tr

    >
  31. </

    thead

    >
  32. <tbody

    id

    =

    "list1"

    >
  33. </

    tbody

    >
  34. </

    table

    >
  35. </

    div

    >
  36. <div

    class

    =

    "col-md-2"

    >
  37. <button

    class

    =

    "btn btn-default"

    id

    =

    "copy"

    onclick

    =

    "copyArray(this);"

    disabled

    =

    "disabled"

    >

    Copy</

    button

    >
  38. <br

    /

    ><br

    /

    >
  39. <button

    class

    =

    "btn btn-default"

    id

    =

    "clear"

    onclick

    =

    "clearAll(this);"

    disabled

    =

    "disabled"

    >

    Reset</

    button

    >
  40. </

    div

    >
  41. <div

    class

    =

    "col-md-3"

    >
  42. <table

    class

    =

    "table table-bordered"

    >
  43. <thead

    class

    =

    "alert-info"

    >
  44. <tr

    >
  45. <th

    >

    Array 2</

    th

    >
  46. </

    tr

    >
  47. </

    thead

    >
  48. <tbody

    id

    =

    "list2"

    >
  49. </

    tbody

    >
  50. </

    table

    >
  51. </

    div

    >

  52. </

    div

    >
  53. </

    body

    >
  54. <script

    src

    =

    "script.js"

    ></

    script

    >
  55. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will copy the array to other array when the button copy is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let list1 =

    document.getElementById

    (

    'list1'

    )

    ;
  2. let array1 =

    [

    ]

    ;
  3. let array2 =

    [

    ]

    ;


  4. function

    arrayList1(

    )

    {
  5. let data =

    ''

    ;
  6. if

    (

    array1.length

    >

    0

    )

    {
  7. for

    (

    var

    i =

    0

    ;

    i <

    array1.length

    ;

    i++

    )

    {
  8. data +=

    '<tr>'

    ;
  9. data +=

    '<td>'

    +

    array1[

    i]

    +

    '</td>'

    ;
  10. data +=

    '</tr>'

    ;
  11. }
  12. }
  13. list1.innerHTML

    =

    data;
  14. }

    ;

  15. function

    arrayList2(

    )

    {
  16. let data =

    ''

    ;
  17. if

    (

    array2.length

    >

    0

    )

    {
  18. for

    (

    var

    i =

    0

    ;

    i <

    array2.length

    ;

    i++

    )

    {
  19. data +=

    '<tr>'

    ;
  20. data +=

    '<td>'

    +

    array2[

    i]

    +

    '</td>'

    ;
  21. data +=

    '</tr>'

    ;
  22. }
  23. }

  24. list2.innerHTML

    =

    data;
  25. }

    ;


  26. function

    copyArray(

    btn)

    {
  27. if

    (

    array1.length

    >

    0

    )

    {
  28. for

    (

    var

    i =

    0

    ;

    i <

    array1.length

    ;

    i++

    )

    {
  29. array2.push

    (

    array1[

    i]

    )

    ;
  30. }

  31. arrayList2(

    )

    ;
  32. }

  33. btn.setAttribute

    (

    "disabled"

    ,

    "disabled"

    )

    ;
  34. document.getElementById

    (

    'clear'

    )

    .removeAttribute

    (

    'disabled'

    )

    ;
  35. }

  36. function

    clearAll(

    btn)

    {
  37. array2=

    [

    ]

    ;
  38. document.getElementById

    (

    'list2'

    )

    .innerHTML

    =

    ""

    ;
  39. document.getElementById

    (

    'copy'

    )

    .removeAttribute

    (

    'disabled'

    )

    ;
  40. btn.setAttribute

    (

    "disabled"

    ,

    "disabled"

    )

    ;
  41. }

  42. function

    insert(

    )

    {
  43. let el =

    document.getElementById

    (

    'name'

    )

    ;
  44. let name =

    el.value

    ;
  45. if

    (

    name)

    {
  46. array1.push

    (

    name.trim

    (

    )

    )

    ;
  47. el.value

    =

    ''

    ;
  48. }
  49. arrayList1(

    )

    ;

  50. if

    (

    array2.length

    ==

    0

    )

    {
  51. document.getElementById

    (

    'copy'

    )

    .removeAttribute

    (

    'disabled'

    )

    ;
  52. }
  53. }

    ;



In the code above we created multiple methods that will handle the array data. In order to copy an array we will first create a method called copyArray(). This function will copy the existing array to an empty array object by pushing using the push() function.

Output:

How%20to%20Copy%20an%20Array%20to%20Other%20Array%20in%20JavaScript%201.png


The How to Copy an Array to Other Array in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Copy an Array to Other Array in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,736

324,744

Top