How to Override an Object from Array of Objects in JavaScript

Introduction

In this tutorial we will create a How to Override an Object from Array of Objects. This tutorial purpose is to provide more ways to used array objects. This will tackle the overriding data of array object. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is very easy to create just follow the instruction and you will do it without a problem. This program can be use to any part of your on working program that need overriding of array object. I will try my best to give you the simplest way of creating this program How to Override an Object from Array of Objects. So let's do the coding.

What is JavaScript Array?

Arrays are a special type of objects that can read any certain type of object such as variable, characters, int, etc. It is also a container of object that holds any fixed number of values of a single type. It can enables the storing a collection of multiple items under a single variable name.

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 display an interface that will list the array object in the HTML page. 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"/>
  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="https://www.sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="container-fluid">
  13. <div class="row">
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">How to Override an Object from Array of Objects</h3>
  17. <hr style="border-top: 1px SOLID #8c8b8b;"/>
  18. <pre id="display"></pre>
  19.  
  20. <form class="form-group">
  21. <div class="form-inline">
  22. <input type="number" id="id" placeholder="Enter ID" class="form-control"/>
  23. <input type="text" id="name" placeholder="Enter Name" class="form-control"/>
  24. <input type="text" id="lastname" placeholder="Enter Lastname" class="form-control"/>
  25. </div>
  26. <br />
  27. <div class="col-md-3"></div>
  28. <div class="col-md-4">
  29. <button type="button" class="btn btn-default btn-block" onclick="update();">Update</button>
  30. </div>
  31. </form>
  32.  
  33. </div>
  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 override the current array object with the new set of array object. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. const data = [{
  2. id: 1,
  3. name: 'John',
  4. lastname: 'Smith'
  5. },
  6. {
  7. id: 2,
  8. name: 'Jill',
  9. lastname: 'Smith'
  10. },{
  11. id: 3,
  12. name: 'Claire',
  13. lastname: 'Temple'
  14. }
  15. ];
  16.  
  17.  
  18. function update(){
  19.  
  20.  
  21. let id=document.getElementById('id');
  22. let name=document.getElementById('name');
  23. let lastname=document.getElementById('lastname');
  24.  
  25. if(id.value!="" && name.value!="" && lastname.value!=""){
  26.  
  27. let newID=parseInt(id.value);
  28.  
  29. const obj={
  30. id: newID,
  31. name: name.value,
  32. lastname: lastname.value
  33. };
  34.  
  35.  
  36.  
  37.  
  38. const foundObj = data.find(({ id }) => id === obj.id);
  39. if (foundObj) {
  40. Object.assign(foundObj, obj);
  41. }else{
  42. console.log("Not Found");
  43. }
  44.  
  45. id.value="";
  46. name.value="";
  47. lastname.value="";
  48.  
  49. document.getElementById("display").innerHTML=JSON.stringify(data, null, 2);
  50. }else{
  51. alert("Please")
  52. }
  53.  
  54. }
  55. document.getElementById("display").innerHTML=JSON.stringify(data, null, 2);

In this code we will display first the array object using the JSON.stringify() function into the html page content. We then bind the inputs value and set the button click event with the onclick function.

To override the array objects we must first append the inputs value into an array object so that it will be easier overriding the array of objects. Then we use the find() function to check and find the id that we will be targeting to override. After we find the target we now then use the Object.assign() function, this will override the current array of object into a new array of object.

Output:

The How to Override an Object from Array of Objects 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 Override an Object from Array of Objects 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