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

How to Filter Object Array Based on Attributes in JavaScript

Cory

Communication Traffic Observer
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
175
Likes
137
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a How to Filter Object Array Based on Attributes. This tutorial purpose is to provide more useful function that can be used in array data. This will tackle the filtering data attributes of array object. I will provide a sample program to show the actual coding of this tutorial. So Let's do the coding.

What is JavaScript Array?

Arrays are a special type of objects where typeof operator in JavaScript returns "object" for array. It is a container object that holds a fixed number of values of a single type. It also enables 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 Filter Object Array Based on Attributes</

    h3

    >
  18. <hr

    style

    =

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

    /

    >
  19. <pre

    id

    =

    "display"

    ></

    pre

    >
  20. <h6

    ><strong

    >

    Note:</

    strong

    >

    Will filter the price if less than the display list<h5

    >
  21. <input

    type

    =

    "text"

    id

    =

    "price"

    placeholder

    =

    "Enter price"

    /

    ><button

    onclick

    =

    "filter();"

    >

    Filter</

    button

    >

    <button

    onclick

    =

    "reset();"

    >

    Reset</

    button

    >
  22. </

    div

    >
  23. </

    div

    >
  24. </

    div

    >
  25. </

    body

    >
  26. <script

    src

    =

    "script.js"

    ></

    script

    >
  27. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically filter the array object base on the given attribute conditional method. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var

    array =

    {
  2. 'mobile'

    :

    [

    {
  3. "mobile_id"

    :

    "1"

    ,
  4. "price"

    :

    "600"

    ,
  5. "brand"

    :

    "samsung"

    ,
  6. "model"

    :

    "Samsung S22"

    ,
  7. }

    ,

    {
  8. "mobile_id"

    :

    "2"

    ,
  9. "price"

    :

    "750"

    ,
  10. "brand"

    :

    "apple"

    ,
  11. "model"

    :

    "Iphone 14"

    ,
  12. }

    ,

    {
  13. "mobile_id"

    :

    "3"

    ,
  14. "price"

    :

    "850"

    ,
  15. "brand"

    :

    "samsung"

    ,
  16. "model"

    :

    "Galaxy Z Fold 4"

    ,
  17. }

    ,

    {
  18. "mobile_id"

    :

    "4"

    ,
  19. "price"

    :

    "450"

    ,
  20. "brand"

    :

    "vivo"

    ,
  21. "model"

    :

    "Vivo V23"

    ,
  22. }

    ,
  23. ]
  24. }

    ;

  25. function

    reset(

    )

    {
  26. document.getElementById

    (

    "display"

    )

    .innerHTML

    =

    JSON.stringify

    (

    array,

    null

    ,

    2

    )

    ;
  27. }


  28. function

    filter(

    )

    {
  29. let price=

    document.getElementById

    (

    "price"

    )

    .value

    ;

  30. if

    (

    typeof

    price !==

    'undefined'

    &&

    price)

    {
  31. let newArray =

    array.mobile

    .filter

    (

    function

    (

    el)

    {
  32. return

    el.price

    >=

    price
  33. }

    )

    ;

  34. document.getElementById

    (

    "display"

    )

    .innerHTML

    =

    JSON.stringify

    (

    newArray,

    null

    ,

    2

    )

    ;
  35. }

    else

    {
  36. alert(

    "Please enter price first!"

    )

    ;
  37. }
  38. }
  39. document.getElementById

    (

    "display"

    )

    .innerHTML

    =

    JSON.stringify

    (

    array,

    null

    ,

    2

    )

    ;

In this code we will display first the array object using the JSON.stringify() function into the html page. To filter the array object we will first bind the button and input text then we use the filter() function that will basically outputs all the element object that pass a specific test or satisfies a specific function. In my example I filter out the price data where the value is greater than the listed value.

Output:

How%20to%20Filter%20Object%20Array%20Based%20on%20Attributes%20in%20JavaScript%201.png


The How to Filter Object Array Based on Attributes 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 Filter Object Array Based on Attributes 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