0

Can you help me please,

i have a problem i have two date type list : listDate: Date[] = []; and listTotalDateProcEnCours: Date[] = [];

I want to insert listDate in the ather list: listTotalDateProcEnCours.

This is my code :

    this.dossiers.forEach((element: any) => {
      const listselected: any[] = [];
      const listProcEnCours: any[] = [];
      const listCommentaire: any[] = [];

      **const listDate: Date[] = [];**

      element.porteuseProcedureEnCours.forEach((e: any) => {
        listProcEnCours.push(e.procedureEnCours)
        if (e.reponse === true) {
          // console.log('true', e.procedureEnCours)
          listselected.push(e.procedureEnCours)
          if (e.commentaire != null) {
            listCommentaire.push(e.commentaire)
          }
          if (e.date != null) {
            listDate.push(e.date);
          }
        }
      });
      **this.listTotalDateProcEnCours.push(listDate);**

When i tried to do that the compiler gives me this error:

Argument of type 'Date[]' is not assignable to parameter of type 'Date'. Type 'Date[]' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.

Can you help me solve this problem, i want to insert date list into another date type list ?

Dhia
  • 17
  • 3

1 Answers1

2

[EDITED]:

Your exception:

Argument of type 'Date[]' is not assignable to parameter of type 'Date'

tells that elements of this.listTotalDateProcEnCours should be of type 'Date'. And when you try to push list with dates into it - compiler shows an error.

If pushing list into another list (two-dimensional array) is your desired behaviour - you should change the type of total list to:

this.listTotalDateProcEnCours: Date[][] or Array<Date[]>

ELSE:

If you want to flatten, You should use spread operator if you want to "extend" one array with another:

this.listTotalDateProcEnCours.push(...listDate)

OR

this.listTotalDateProcEnCours = this.listTotalDateProcEnCours.concat(listDate);

OR

Array.prototype.push.apply(this.listTotalDateProcEnCours,listDate)

Decide which way is cleaner for you.

Useful link: How to extend an existing JavaScript array with another array, without creating a new array

Vitalii Y
  • 131
  • 5
  • thank you for your answer sir, i want to have a list set in the total list, that way, I just concatenate the 2 lists that's all. this is not my goal, my goal is to have several list of dates in a total list i.e. the total list will have several other lists => two-dimensional array – Dhia Mar 22 '21 at 11:33
  • @Dhia, Okay. Now I got what you want. This means that you have to change the type of your total list to `this.listTotalDateProcEnCours: Date[][]` or `Array`. The second one seems to me more obvious. – Vitalii Y Mar 22 '21 at 13:15