How to Add New Entry to Multidimensional Array in JavaScript

How to Add New Entry to Multidimensional Array in JavaScript


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

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. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Add New Entry to Multidimensional Array</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <pre>[<div id="result"></div>]</pre>
  18. </div>
  19. <div class="col-md-4">
  20. <div class="form-group">
  21. <label>Firstname</label>
  22. <input type="text" id="firstname" class="form-control"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Lastname</label>
  26. <input type="text" id="lastname" class="form-control"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Address</label>
  30. <input type="text" id="address" class="form-control"/>
  31. </div>
  33. <center><button type="button" onclick="addEntry()" class="btn btn-primary form-control">Add</button></center>
  35. </div>
  36. </div>
  37. </body>
  38. <script src="script.js"></script>
  39. </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. ];
  5. displayResult(data);
  8. function addEntry(){
  9. let firstname = document.getElementById("firstname");
  10. let lastname = document.getElementById("lastname");
  11. let address = document.getElementById("address");
  13. if(firstname.value == "" || lastname.value == "" || address.value == ""){
  14. alert("Please complete the required field first!");
  15. }else{
  16. var member = {
  17. 'firstname': firstname.value,
  18. 'lastname': lastname.value,
  19. 'address': address.value,
  20. };
  22. data.push(member);
  25. displayResult(data);
  27. }
  28. }
  30. function displayResult(data){
  31. let html = "" ;
  33. for(var i in data){
  34. html += "&nbsp;&nbsp;&nbsp;{firstname:"+ "'"+data[i].firstname+"'"+", "+"lastname:'"+data[i].lastname+"'"+", "+"address:'"+data[i].address+"'"+"}<br />" ;
  35. }
  37. document.getElementById("result").innerHTML = html;
  38. firstname.value = "";
  39. lastname.value = "";
  40. address.value = "";
  41. }

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.


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

Add new comment