Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Search inside child Array of objects with multiple value

Currently i have below Array of Objects

const dataClass = [
    {
        "id": 101,
        "class": [
            {
                "type": "A",
                "value": "A-class"
            },
            {
                "type": "B",
                "value": "B-class"
            },
            {
                "type": "C",
                "value": "C-class"
            }
        ],
        "rank": 1
    },
    {
        "id": 102,
        "class": [
            {
                "type": "D",
                "value": "D-class"
            },
            {
                "type": "E",
                "value": "E-class"
            },
            {
                "type": "F",
                "value": "F-class"
            }
        ],
        "rank": 2
    },
    {
        "id": 103,
        "class": [
            {
                "type": "G",
                "value": "G-class"
            },
            {
                "type": "H",
                "value": "H-class"
            },
            {
                "type": "I",
                "value": "I-class"
            }
        ],
        "rank": 3
    }
];

i need to get dataClass object using all value inside the class object, let say i want to get the second object, so i have to search/input "type": "D", "type": "E", and "type": "F".

return array object/object i expect:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    [{
        "id": 102,
        "class": [
            {
                "type": "D",
                "value": "D-class"
            },
            {
                "type": "E",
                "value": "E-class"
            },
            {
                "type": "F",
                "value": "F-class"
            }
        ],
        "rank": 2
    }]

I don’t find any solution so far, Thanks for any help.

>Solution :

Hope this will help,

const dataClass = [
    {
        "id": 101,
        "class": [
            {
                "type": "A",
                "value": "A-class"
            },
            {
                "type": "B",
                "value": "B-class"
            },
            {
                "type": "C",
                "value": "C-class"
            }
        ],
        "rank": 1
    },
    {
        "id": 102,
        "class": [
            {
                "type": "D",
                "value": "D-class"
            },
            {
                "type": "E",
                "value": "E-class"
            },
            {
                "type": "F",
                "value": "F-class"
            }
        ],
        "rank": 2
    },
    {
        "id": 103,
        "class": [
            {
                "type": "G",
                "value": "G-class"
            },
            {
                "type": "H",
                "value": "H-class"
            },
            {
                "type": "I",
                "value": "I-class"
            }
        ],
        "rank": 3
    }
];

const resultArr = [];

for (const ch_arr of dataClass){
  for (const class_arr of ch_arr["class"]){
      if(["D","E","F"].includes(class_arr["type"])){
        resultArr.push(ch_arr);
        break;
      }
    };
};

// resultArr is the expected array
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading