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

How to separate an array into two arrays

I have an array of objects. I want to remove Fields which have columns has null. It has to separate into two different arrays which has columns data another has does not have columns

const groupItems = [
    {
        "GroupName": "CorrSection",
        "Fields": [
            {
                "FieldName": "TemplateVersion",
                "Columns": null
            },
            {
                "FieldName": "CorrTypePrintGrps",
                "Columns": [
                    {
                        "ColumnName": "CorrTypeName"
                    }
                ]
            }
        ]
    },
    {
        "GroupName": "RcSection",
        "Fields": [
            {
                "FieldName": "RcVersion",
                "Columns": null
            },
            {
                "FieldName": "RcPrintGrps",
                "Columns": [
                    {
                        "ColumnName": "RcName"
                    }
                ]
            }
        ]
    }
]

I tried to filter the objects but unable to get parent object along with it, to be precise GroupName is missing

groupItems.map(item=>item.Fields.filter(k=>k.Columns!==null))

I am expecting two different arrays as output

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

Array_One

[
    {
        "GroupName": "CorrSection",
        "Fields": [
            {
                "FieldName": "TemplateVersion",
                "Columns": null
            }
        ]
    },
    {
        "GroupName": "RcSection",
        "Fields": [
            {
                "FieldName": "RcVersion",
                "Columns": null
            }
        ]
    }
]

Array_Two

[
    {
        "GroupName": "CorrSection",
        "Fields": [
            {
                "FieldName": "CorrTypePrintGrps",
                "Columns": [
                    {
                        "ColumnName": "CorrTypeName"
                    }
                ]
            }
        ]
    },
    {
        "GroupName": "RcSection",
        "Fields": [
            {
                "FieldName": "RcPrintGrps",
                "Columns": [
                    {
                        "ColumnName": "RcName"
                    }
                ]
            }
        ]
    }
]

>Solution :

You can use map with destructuring to extract fields:

const groupItems = [
    {
        "GroupName": "CorrSection",
        "Fields": [
            {
                "FieldName": "TemplateVersion",
                "Columns": null
            },
            {
                "FieldName": "CorrTypePrintGrps",
                "Columns": [
                    {
                        "ColumnName": "CorrTypeName"
                    }
                ]
            }
        ]
    },
    {
        "GroupName": "RcSection",
        "Fields": [
            {
                "FieldName": "RcVersion",
                "Columns": null
            },
            {
                "FieldName": "RcPrintGrps",
                "Columns": [
                    {
                        "ColumnName": "RcName"
                    }
                ]
            }
        ]
    }
]


const arr1 = groupItems.map(({ GroupName, Fields }) => ({
  GroupName,
  Fields: Fields.filter(({ Columns }) => Columns === null)
}));

const arr2 = groupItems.map(({ GroupName, Fields }) => ({
  GroupName,
  Fields: Fields.filter(({ Columns }) => Columns !== null)
}));


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