0
votes

I have a error like this... The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

I have shown in MDN explanation and I am not finding where is the problem. When I delete the month from this.Months.push then the sorting it is working but it is showing in dropdown only numbers not the text with months there. Here is my code.

This is the HTML.

<app-dropdown [ngModel]="selectedMonths" [multiSelect]="true" label="Select Month" labelWidth="75px" optionWidth="150px" [items]="Months" (ngModelChage)="monthChange($event)">
      </app-dropdown>

This is the TS

 purchaser: Map<string, Purchased>;

 months: number [] = [];
 Months: DropdownOption[] = [];


const monate = [
        'January',
       'February',
        'March',
        'Aprill',
         'Mai',
        'June',
        'July',
        'August',
        'September',
        'October',
        'November',
        'December'
      ];

this.purchaser.forEach(purchaserId => {
        if (!this.months.includes(purchaserId.created.getMonth())) {
          this.months.push(purchaserId.created.getMonth());
          this.Months.push({key: purchaserId.created.getFullYear().toString(), value: month[purchaserId.created.getMonth().toString()]});
        }
      });

this.Months.sort((a, b) => +a.value - +b.value);
this.months.sort((a, b) => a - b);
month.sort((a, b) => a - b); // here is saying me the failure.
1
month, months, Months, monate — have you considered some basic cleanup? And obviously the issue is, that month is not a number array.qqilihq
Is it a function ? Can you share more context ?Sergio Escudero
@qqilihq I know that month is not a number but how can I fix this.Abedin.Zhuniqi
@saescudero Purchaser are an interface and when a new purchase is added takes a date but in a dropdwon when i call this purchaser with Months = dropdownoption you can see in the code I push the date in the month and there I call the months which are created when a purchaser is added, without this month everything it is working but show me numbers in the dropdwon not the text of the monthsAbedin.Zhuniqi
Can you be more specific what you want to “fix”?? Your snippet contains lots of irrelevant code, yet misses relevant parts.qqilihq

1 Answers

0
votes

@ZhuniquA. The error is clear,

//You can use
a=+"23" //a=23

//But
a=+"potatoe"  //Give an error

If you want to sort the months by name you use

this.monate.sort((a,b)=>{
     return a==b?0:a>b?1:-1;
})

But it's strange you want to sort "some" with month by the name of the month :(

When we are using some like "month", The "key" is that "month" was a number. If you want to show the month name you can, e.g.

//Supouse your items are like [{data:"A",month:1}{data:"B",month:10}...]
//and you have defined in your .ts monate=['January','Febrary'......] 

<div *ngFor="let item of items">
    {{item.data}}{{monate[item.month-1]}}
</div>

Or

//you can have an object item like {data:'A',month:3} and a form like

<form>
   <input [(ngModel)]="item.data"/>
   <select [(ngModel)]="item.month">
        <option *ngFor="let month of monate;let i=index" [value]="{{i+1}}"> 
             {{month}}
        </option>
   </select>
</form>

One thing is the "data" an another one is how you show the data