如何访问对象的子数组并将其推入父数组

时间:2018-12-19 15:32:30

标签: arrays angular

我正在尝试将子数组推到父数组并检查子数组的值,我该如何做到这一点或实现这一目的的最佳方法?

界面

export interface IErsaApps {
        app_id: number;
        app_type_id: number;
        app_name: string;
        app_roles: string;
        app_sort_id?: number;
        selectedApp: boolean;
        seletedAppRoleID?: number;
        roles: Array<IErsaAppRoles>;
}
export interface IErsaAppRoles {
    app_role_id: number;
    app_role_app_id: number;
    app_role_name: string;
    app_role_sort_id?: number;   
}
export interface IErsaPreviewApp {
    app_type_id: number;
    apps: Array<IErsaApps>;
}

TS (我可以第一次推送到数组,但是之后需要推送到子数组)

   selectedObject: IErsaApps; 
      iErsaDefaultApps: IErsaApps[] =[]; 
      iErsaPrevSelectedApps: IErsaPreviewApp[] = [];
    toggleSelectedApp(event: any, rowIndexValue: any)
        { 

         this.selectedObject = this.iErsaAppList .find(x => x.app_id == event.srcElement.value);

    //This work        
    this.iErsaPrevSelectedApps.splice(0, 0, { "app_type_id": this.selectedObject.app_type_id, "apps": [this.selectedObject] });

    //trying to push just the child, how do I do that?              
                        this.iErsaPrevSelectedApps.splice(1, 0, { "apps": [this.selectedObject] });


}

1 个答案:

答案 0 :(得分:1)

如果要将对象推入apps(子级)Array中,可以采用以下方式:

this.iErsaPrevSelectedApps[1].apps.splice(0, 0, this.selectedObject);

注释:

根据您的示例使用索引1:this.iErsaPrevSelectedApps[1]

在此处插入索引0处的对象:splice(0, 0, this.selectedObject);,但是您可以根据需要进行调整。