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

TypeScript: add a new object into an array of objects

I have the following data structure:

uiBundles:
[
    {
        "id": "tasks widget UI",
        "uiUnits": [
            {
                "type": "widget",
                "id": "tasks-widget",
                "roles": "MB"
            }
        ]
    },
    {
        "id": "berater widget UI",
        "uiUnits": [
            {
                "type": "widget",
                "id": "berater-widget",
                "roles": "MB"
            }
        ]
    }
]

What I would like to do is add a new uiUnit into this embedded array of objects. Here is my code:

add-new.component.ts:

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

uiBundles: UIBUndle[];

ngOnInit(): void {
    this.getBundlesService.getUiBundles().subscribe((value: UIBundle[]) => this.uiBundles = value);
}

addWidget(id: string): void {
    this.selectedUiUnits = this.uiBundles.filter((data) => data.id === id);
    let newWidget = { id: 'new', uiUnits: [{ id: 'new-widget', type: 'widget', roles:'MB' }] };
}

add-new.component.html:

<div *ngFor="let bundle of uiBundles">
  <button (click)="addWidget(bundle.id)"></button>
</div>

When I run this code, the result is this:

[
    {
        "id": "tasks widget UI",
        "uiUnits": [
            {
                "type": "widget",
                "id": "tasks-widget",
                "roles": "MB"
            }
        ]
    },
    {
        "id": "berater widget UI",
        "uiUnits": [
            {
                "type": "widget",
                "id": "berater-widget",
                "roles": "MB"
            }
        ]
    },
    {
        "id": "new",
        "uiUnits": [
            {
                "type": "widget",
                "id": "new-widget",
                "roles": "MB"
            }
        ]
    }
]

But what I am trying to do would be:

[
    {
        "id": "tasks widget UI",
        "uiUnits": [
            {
                "type": "widget",
                "id": "tasks-widget",
                "roles": "MB"
            },
            {
                "type": "widget",
                "id": "new widget",
                "roles": "MB"
            }
        ]
    },
    {
        "id": "berater widget UI",
        "uiUnits": [
            {
                "type": "widget",
                "id": "berater-widget",
                "roles": "MB"
            }
        ]
    }
]

Can someone please help me, what did I do wrong here?

>Solution :

Try this:

addWidget(id: string): void {
    const index: number = this.uiBundles.findIndex((data) => data.id === id);
    const newUnits = [{ id: 'new-widget', type: 'widget', roles:'MB' }];

    this.uiBundles[index].uiUnits.push(newUnits);
}
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