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

时间:2018-08-26 14:51:51

标签: javascript arrays angular typescript

我使用有角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() ) {}
}

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

只需使用地图

{{1}}