0

I use angular CLI 6 and firebase. I obtain in type script this list of array ;

(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)

I want to convert the first one to this type of object :

(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)

I try this :

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);
    });
});

ERROR : expect 3 - 5 argument but got 0

Ppsplan in my interface :

export class Ppsplan {
  constructor(public typetraitement:string, public acte:string, public duree:number, public datedebut = new Date() , public datefin = new Date() ) {}
}
2
  • 1
    What have you tried and what exactly is the problem with it? Commented Aug 26, 2018 at 14:56
  • i update, thkss Commented Aug 26, 2018 at 15:05

2 Answers 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);
Sign up to request clarification or add additional context in comments.

4 Comments

@KaiserKatze you could use the spread operator new Ppsplan(...list) instead of list[0], list[1], ... etc.
@alNTM What do you mean by "You can't change directly your tscode"?
@KaiserKatze Existing an another way than my subscribe method (in my question)?
@alNTM I think the problematic part in your code is this: data.push([ ...interestingFields.map(field => ppsplan[field]) ]);. @see Array.prototype.push. Without knowing what interestingFields, I can only guess that you should have written like this: data.push( ...interestingFields.map(field => ppsplan[field]) );. Please note that I remove the pair of brackets around ...interestingFields.map(field => ppsplan[field]).
1

Just use map

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
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.