将数组列表转换为对象数组

问题描述 投票:0回答:2

我使用角度CLI 6和firebase。我在类型脚本中获得了这个数组列表;

(3) [Array(5), Array(5), Array(5)]
     0 : (5) ["Chimiothérapie", "rettreret", 4, 1534716000000, 1535061600000]
     1 : (5) ["Chimiothérapie", "trferterteretretetr", 3, 1535061600000, 1535320800000]
     2 : (5) ["Chimiothérapie", "zrteretetrertertgerter", 4, 1534370400000, 1534716000000]
     length: 3__proto__ : Array(0)

我想将第一个转换为这种类型的对象:

(3) [Ppsplan, Ppsplan, Ppsplan]
 0 : Ppsplan {typetraitement: "Chimiothérapie", acte: "rettreret", duree: 4, datedebut: 1534802400000, datefin: 1535148000000}
 1 : Ppsplan {typetraitement: "Chimiothérapie", acte: "trferterteretretetr", duree: "", datedebut: 1534111200000, datefin: 1534111200000}
 2 : Ppsplan {typetraitement: "Chimiothérapie", acte: "zrteretetrertertgerter", duree: 3, datedebut: 1535493600000, datefin: 1535493600000}
length :3
 __proto__ :Array(0)

我试试这个:

this.ppssToDisplay2 = this.ppssService.getSinglePPS2(this.key);
this.ppssToDisplay2.subscribe((ppsList: PPS[]) => {
    console.log(ppsList);

    let data =[];

    ppsList.map((pps: PPS) => {
        Object.keys(pps.ppsplan)
            .forEach(key => {
                let ppsplan: Ppsplan = pps.ppsplan[key];
                data.push([ ...interestingFields.map(field => ppsplan[field]) ]);

                const convert_data= (list: any[]) => new Ppsplan(...list);
                let ppsplans = [];
                for (let list of data) {
                    ppsplans.push(convert_data(list));
                }
            });

        console.log(this.ppsplans);
    });
});

错误:期望3 - 5参数,但得到0

Ppsplan在我的界面中:

export class Ppsplan {
  constructor(public typetraitement:string, public acte:string, public duree:number, public datedebut = new Date() , public datefin = new Date() ) {}
}
javascript arrays angular typescript
2个回答
1
投票
let list_of_list = [
    ["Chimiothérapie", "rettreret", 4, 1534716000000, 1535061600000],
    ["Chimiothérapie", "trferterteretretetr", 3, 1535061600000, 1535320800000],
    ["Chimiothérapie", "zrteretetrertertgerter", 4, 1534370400000, 1534716000000],
];

export class Ppsplan {
    constructor(
        public typetraitement: string,
        public acte: string,
        public duree: number,
        public datedebut = new Date() ,
        public datefin = new Date()
    ) {}
}

const convert_list_to_object = (list: any[]) => new Ppsplan(...list);

let list_of_objects = [];
for (let list of list_of_list) {
    list_of_objects.push(convert_list_to_object(list));
}

console.log(list_of_objects);

0
投票

只需使用地图

let list_of_list = [
    ["Chimiothérapie", "rettreret", 4, 1534716000000, 1535061600000],
    ["Chimiothérapie", "trferterteretretetr", 3, 1535061600000, 1535320800000],
    ["Chimiothérapie", "zrteretetrertertgerter", 4, 1534370400000, 1534716000000],
];

mylist=list_of_list.map(x=>{
   let obj={
       typetraitement: x[0], 
       acte: x[1],
       duree: x[2],
       datedebut:x[3],
       datefin:x[4]
   }
   return obj
})
© www.soinside.com 2019 - 2024. All rights reserved.