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

Remove object from a list of arrays if field in object equals a certain value javascript

I have an array of objects and within those objects, I have another array. I want to filter the results by the checked value in the drilled-down array.

I have tried map, forEach, splice and I think I am missing something.

[
    {
        "id": 1,
        "parent": "Parent 1",
        "parents": [
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": true
            },
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": false
            },
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": true
            },
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": false
            }
        ]
    },
    {
        "id": 1,
        "parent": "Parent 2",
        "parents": [
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": true
            },
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": false
            },
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": true
            },
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": false
            }
        ]
    }
]

The results I am trying to get:

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": 1,
        "parent": "Parent 1",
        "parents": [
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": true
            },
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": true
            }
        ]
    },
    {
        "id": 1,
        "parent": "Parent 2",
        "parents": [
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": true
            },
            {
                "label": "New Item",
                "editable": true,
                "children": [],
                "checked": true
            }
        ]
    }
]

>Solution :

A combination of .map() and .filter() should do the job:

const result = array.map((el) => ({
  ...el,
  parents: el.parents.filter((c) => c.checked) 
}));

console.log(result);
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