0

This is my multi dimensional array. Output should be in array2 structure. I have tried many ways covert above array in easy but all came up with bulky code and lots of iteration. Is there any easy way to do so?

I am a new in angular and JavaScript.

var array1=[
    {
        "id": 1,
        "name": "Johny",
        "category": [
            {
                "id": 12,
                "name": "GUI",
                "status": {
                    "id": 123,
                    "status": "working",
                    "name": "GUI"
                }
            },
            {
                "id": 13,
                "name": "GFX",
                "status": {
                    "id": 124,
                    "status": "working",
                    "name": "GFX"
                }
            },
            {
                "id": 14,
                "name": "UI",
                "status": {
                    "id": 125,
                    "status": "working",
                    "name": "UI"
                }
            }
        ]
    },
    {
        "id": 2,
        "name": "Paul",
        "category": [
            {
                "id": 21,
                "name": "GUI",
                "status": {
                    "id": 212,
                    "status": "Progress",
                    "name": "GUI"
                }
            },
            {
                "id": 22,
                "name": "GFX",
                "status": {
                    "id": 221,
                    "status": "working",
                    "name": "GFX"
                }
            },
            {
                "id": 23,
                "name": "DM",
                "status": {
                    "id": 231,
                    "status": "done",
                    "name": "DM"
                }
            }
        ]
    }
]

I want to change into this array2.

var array2=[
    {
        "id": 1,
        "name": "Johny",
        "category": [
            {
                "id": 12,
                "name": "GUI",
                "data": [
                    {
                        "id": 12,
                        "name": "GUI",
                        "status": {
                            "id": 123,
                            "status": "working",
                            "name": "GUI"
                        }
                    },
                    {
                        "id": 21,
                        "name": "GUI",
                        "status": {
                            "id": 212,
                            "status": "Progress",
                            "name": "GUI"
                        }
                    }
                ]
            },
            {
                "id": 13,
                "name": "GFX",
                "data": [
                    {
                        "id": 13,
                        "name": "GFX",
                        "status": {
                            "id": 124,
                            "status": "working",
                            "name": "GFX"
                        }
                    },
                    {
                        "id": 22,
                        "name": "GFX",
                        "status": {
                            "id": 221,
                            "status": "working",
                            "name": "GFX"
                        }
                    }
                ]
            },
            {
                "id": 14,
                "name": "UI",
                "data": [
                    {
                        "id": 14,
                        "name": "UI",
                        "status": {
                            "id": 125,
                            "status": "working",
                            "name": "UI"
                        }
                    },
                    //null data if not in first
                    {}
                ]
            },
            {
                "id": 23,
                "name": "DM",
                "data": [
                    //null data if not in first
                    {},
                    {
                        "id": 23,
                        "name": "DM",
                        "status": {
                            "id": 231,
                            "status": "done",
                            "name": "DM"
                        }
                    }
                ]
            }
        ]
    },
    {
        "id": 2,
        "name": "Paul",
        "category": [
            {
                "id": 21,
                "name": "GUI",
                "data": [
                    {
                        "id": 12,
                        "name": "GUI",
                        "status": {
                            "id": 123,
                            "status": "working",
                            "name": "GUI"
                        }
                    },
                    {
                        "id": 21,
                        "name": "GUI",
                        "status": {
                            "id": 212,
                            "status": "Progress",
                            "name": "GUI"
                        }
                    }
                ]
            },
            {
                "id": 22,
                "name": "GFX",
                "data": [
                    {
                        "id": 13,
                        "name": "GFX",
                        "status": {
                            "id": 124,
                            "status": "working",
                            "name": "GFX"
                        }
                    },
                    {
                        "id": 22,
                        "name": "GFX",
                        "status": {
                            "id": 221,
                            "status": "working",
                            "name": "GFX"
                        }
                    }
                ]
            },
            {
                "id": 14,
                "name": "UI",
                "data": [
                    {
                        "id": 14,
                        "name": "UI",
                        "status": {
                            "id": 125,
                            "status": "working",
                            "name": "UI"
                        }
                    },
                    //null data if not in first
                    {}
                ]
            },
            {
                "id": 23,
                "name": "DM",
                "data": [
                    //null data if not in first
                    {},
                    {
                        "id": 23,
                        "name": "DM",
                        "status": {
                            "id": 231,
                            "status": "done",
                            "name": "DM"
                        }
                    }
                ]
            }
        ]
    }
]

Output should be in array2 structure.

Thanks in advance.

1 Answer 1

1

So you just want to have the original data as additional data attribute? This is simple using a forEach() loop in combination with the map() function:

array1.forEach((obj) => {
    obj.category = obj.category.map((cat) => {
    return {id: cat.id, name: cat.name, data: cat}
  });
})

Hope this is what you're looking for :-)

Sign up to request clarification or add additional context in comments.

1 Comment

I need additional modification in this on attribute data i want to push category object if key(name) value present in that category array else push null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.