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

How to Add New Entry to Multidimensional Array in JavaScript

Hdealer

Decentralized Identity Advocate
Divine
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
148
Likes
126
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Add New Entry to Multidimensional Array in JavaScript

Introduction

In this tutorial we will create a How to Add New Entry to Multidimensional Array in JavaScript. This tutorial purpose is to teach you how to add new entry in multidimensional array This will cover all the important functionality that allow you add new multidimensional array entry. 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 if you to store a data in a multidimensional array. I will give my best to provide you the easiest way of creating this program Add New Multidimensional Array Entry. So let's do the coding.

Before we get started:

Here 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 display the html form inputs and the container for the array. 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 Add New Entry to Multidimensional Array</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-8"

    >
  18. <pre

    >

    [<div

    id

    =

    "result"

    ></

    div

    >

    ]</

    pre

    >
  19. </

    div

    >
  20. <div

    class

    =

    "col-md-4"

    >
  21. <div

    class

    =

    "form-group"

    >
  22. <label

    >

    Firstname</

    label

    >
  23. <input

    type

    =

    "text"

    id

    =

    "firstname"

    class

    =

    "form-control"

    /

    >
  24. </

    div

    >
  25. <div

    class

    =

    "form-group"

    >
  26. <label

    >

    Lastname</

    label

    >
  27. <input

    type

    =

    "text"

    id

    =

    "lastname"

    class

    =

    "form-control"

    /

    >
  28. </

    div

    >
  29. <div

    class

    =

    "form-group"

    >
  30. <label

    >

    Address</

    label

    >
  31. <input

    type

    =

    "text"

    id

    =

    "address"

    class

    =

    "form-control"

    /

    >
  32. </

    div

    >

  33. <center><button

    type

    =

    "button"

    onclick

    =

    "addEntry()"

    class

    =

    "btn btn-primary form-control"

    >

    Add</

    button

    ></

    center>

  34. </

    div

    >
  35. </

    div

    >
  36. </

    body

    >
  37. <script

    src

    =

    "script.js"

    ></

    script

    >
  38. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will add new multidimensional array when you submit the form. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var

    data =

    [
  2. {

    firstname:

    "John"

    ,

    lastname:

    "Smith"

    ,

    address:

    "New York"

    }
  3. ]

    ;

  4. displayResult(

    data)

    ;


  5. function

    addEntry(

    )

    {
  6. let firstname =

    document.getElementById

    (

    "firstname"

    )

    ;
  7. let lastname =

    document.getElementById

    (

    "lastname"

    )

    ;
  8. let address =

    document.getElementById

    (

    "address"

    )

    ;

  9. if

    (

    firstname.value

    ==

    ""

    ||

    lastname.value

    ==

    ""

    ||

    address.value

    ==

    ""

    )

    {
  10. alert(

    "Please complete the required field first!"

    )

    ;
  11. }

    else

    {
  12. var

    member =

    {
  13. 'firstname'

    :

    firstname.value

    ,
  14. 'lastname'

    :

    lastname.value

    ,
  15. 'address'

    :

    address.value

    ,
  16. }

    ;

  17. data.push

    (

    member)

    ;


  18. displayResult(

    data)

    ;

  19. }
  20. }

  21. function

    displayResult(

    data)

    {
  22. let html =

    ""

    ;

  23. for

    (

    var

    i in

    data)

    {
  24. html +=

    "&nbsp;&nbsp;&nbsp;{firstname:"

    +

    "'"

    +

    data[

    i]

    .firstname

    +

    "'"

    +

    ", "

    +

    "lastname:'"

    +

    data[

    i]

    .lastname

    +

    "'"

    +

    ", "

    +

    "address:'"

    +

    data[

    i]

    .address

    +

    "'"

    +

    "}<br />"

    ;
  25. }

  26. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    html;
  27. firstname.value

    =

    ""

    ;
  28. lastname.value

    =

    ""

    ;
  29. address.value

    =

    ""

    ;
  30. }

In the code above above we just create a one method that will add new entry to the multidimensional array object called addEntry(). This function will automatically add the entry to the array object after you submitted the html form. The code is consist of array appending usingpush() function to append the array from the forms into the multidimensional array object.

Output:

How%20to%20Add%20New%20Entry%20to%20Multidimensional%20Array%20in%20JavaScript%201.png

The How to Add New Entry to Multidimensional 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 Add New Entry to Multidimensional 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

325,378

325,386

Top