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

How to Override an Object from Array of Objects in JavaScript

BrutalCloud

DeFi Pro
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
156
Likes
198
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://www.sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "container-fluid"

    >
  14. <div

    class

    =

    "row"

    >
  15. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  16. <div

    class

    =

    "col-md-6 well"

    >
  17. <h3

    class

    =

    "text-primary"

    >

    How to Override an Object from Array of Objects</

    h3

    >
  18. <hr

    style

    =

    "border-top: 1px SOLID #8c8b8b;"

    /

    >
  19. <pre

    id

    =

    "display"

    ></

    pre

    >

  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. </

    div

    >
  33. </

    div

    >
  34. </

    div

    >
  35. </

    body

    >
  36. <script

    src

    =

    "script.js"

    ></

    script

    >
  37. </

    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. function

    update(

    )

    {


  17. let id=

    document.getElementById

    (

    'id'

    )

    ;
  18. let name=

    document.getElementById

    (

    'name'

    )

    ;
  19. let lastname=

    document.getElementById

    (

    'lastname'

    )

    ;

  20. if

    (

    id.value

    !=

    ""

    &&

    name.value

    !=

    ""

    &&

    lastname.value

    !=

    ""

    )

    {

  21. let newID=

    parseInt(

    id.value

    )

    ;

  22. const

    obj=

    {
  23. id:

    newID,
  24. name:

    name.value

    ,
  25. lastname:

    lastname.value
  26. }

    ;




  27. const

    foundObj =

    data.find

    (

    (

    {

    id }

    )

    =>

    id ===

    obj.id

    )

    ;
  28. if

    (

    foundObj)

    {
  29. Object

    .assign

    (

    foundObj,

    obj)

    ;
  30. }

    else

    {
  31. console.log

    (

    "Not Found"

    )

    ;
  32. }

  33. id.value

    =

    ""

    ;
  34. name.value

    =

    ""

    ;
  35. lastname.value

    =

    ""

    ;

  36. document.getElementById

    (

    "display"

    )

    .innerHTML

    =

    JSON.stringify

    (

    data,

    null

    ,

    2

    )

    ;
  37. }

    else

    {
  38. alert(

    "Please"

    )
  39. }

  40. }
  41. 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:

How%20to%20Override%20an%20Object%20from%20Array%20of%20Objects%20in%20JavaScript%201.png


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


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

442,401

317,942

317,951

Top