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

How to Combine Two Array Into One Object in JavaScript

Bestdude101

Payload Expert
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
44
Likes
59
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
How to Combine Two Array Into One Object in JavaScript

Introduction

In this tutorial we will create a How to Combine Two Array Into One Object in JavaScript. This tutorial purpose is to teach you how to combine your two array into one. This will tackle all the basic function that will combine the array you have. 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 uses array as your data. I will give my best to provide you the easiest way of creating this program Combine array. 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 array list form and button. 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"

    >

    Sourcecodester</

    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 Combine Two Array Into One Object</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <h3

    >

    Array 1</

    h3

    >
  19. <pre

    id

    =

    "result1"

    ></

    pre

    >

  20. <br

    /

    >

  21. <h3

    >

    Array 2</

    h3

    >
  22. <pre

    id

    =

    "result2"

    ></

    pre

    >
  23. </

    div

    >
  24. <div

    class

    =

    "col-md-6"

    >
  25. <center><button

    class

    =

    "btn btn-primary"

    id

    =

    "merge"

    >

    Merge</

    button

    ></

    center>
  26. <br

    /

    >
  27. <table

    class

    =

    "table table-bordered"

    >
  28. <thead

    >
  29. <tr

    >
  30. <th

    >

    New Array</

    th

    >
  31. </

    tr

    >
  32. </

    thead

    >
  33. <tbody

    id

    =

    "display"

    ></

    tbody

    >
  34. </

    table

    >
  35. </

    div

    >

  36. </

    div

    >
  37. <script

    src

    =

    "script.js"

    ></

    script

    >
  38. </

    body

    >
  39. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will combine your two array when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var

    arr1 =

    [

    "Elephant"

    ,

    "Cat"

    ,

    "Lion"

    ,

    "Bear"

    ]

    ;
  2. var

    arr2 =

    [

    "Dog"

    ,

    "Giraffe"

    ,

    "Tiger"

    ,

    "Eagle"

    ]

    ;


  3. function

    displayArray(

    array1,

    array2)

    {
  4. document.getElementById

    (

    'result1'

    )

    .innerHTML

    =

    array1;
  5. document.getElementById

    (

    'result2'

    )

    .innerHTML

    =

    array2;
  6. }


  7. function

    combineArray(

    array1,

    array2)

    {
  8. var

    merge =

    array1.concat

    (

    array2)

    ;
  9. var

    html =

    ""

    ;
  10. for

    (

    var

    i=

    0

    ;

    i<

    merge.length

    ;

    i++

    )

    {
  11. html+=

    "<tr>"

    ;
  12. html+=

    "<td>"

    +

    merge[

    i]

    +

    "</td>"

    ;
  13. html+=

    "</tr>"

    ;
  14. }

  15. document.getElementById

    (

    'display'

    )

    .innerHTML

    =

    html;
  16. }

  17. displayArray(

    arr1,

    arr2)

    ;

  18. document.getElementById

    (

    'merge'

    )

    .addEventListener

    (

    'click'

    ,

    combineArray.bind

    (

    this

    ,

    arr1,

    arr2)

    )

    ;

In the code above we first create our two array and set it into a variables. We will now then create a method that will combine array into one, we will call it combineArray(). This function will combine your two array using the concat() function, this will take two array to merge into one array.

Output:

How%20to%20Combine%20Two%20Array%20Into%20One%20Object%20in%20JavaScript%201.png


The How to Combine Two Array Into One Object 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 Combine Two Array Into One Object 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