如何将对象列表复制到另一个[Typescript]

Bun*_*kul 2 typescript-typings angular

我只想将值从“全部”复制到“可用”,有时我也无法从可用中获取价值。

我尝试了很多方法,例如slice()Array.from()=它不起作用

let a = [{"key":66,"value":"MAAA","username":"MAZA007"},{"key":66,"value":"MAAA","username":"MAZA007"}];

let all = a
let available = all
Run Code Online (Sandbox Code Playgroud)

我的变量没有值

这是我的完整功能

          protected  async processMoverBox(projectId?:number) {


            let available;
            let all;

            if(projectId != undefined)    
            {

                let projectTeamId =  await this.restful.get("api/operation/S200100/allprojectteam/"+projectId); 
                    console.log(projectTeamId)

                     all = projectTeamId.map((id) => {return this.copy(this.allUser.find(allUser => allUser.key == id))})

            }  
            else{ all = this.copy(this.allUser); console.log(this.allUser)}
            console.log(all);

            let a = [{"key":66,"value":"MAAA","username":"MAZA007"},{"key":66,"value":"MAAA","username":"MAZA007"}]


            available = [...a];

            console.log(available);
            console.log(JSON.stringify(available, null, 4));

            if(this.selected.length>0){
            this.selected.forEach((ms)=>{
                available.splice(available.findIndex(e => e.key == ms.key), 1);
            })
             }

             console.log(available);
             console.log(this.selected)

            this.memberDualbox.initMoveBox(available, this.selected, all)
            this.memberDualbox.disabled = this.isViewMode;
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以像下面的示例一样分配值。

let a = [{"key":66,"value":"MAAA","username":"MAZA007"},{"key":66,"value":"MAAA","username":"MAZA007"}];

let all = a
let available = Object.assign({}, all);
Run Code Online (Sandbox Code Playgroud)