如何修复打字稿中的二维数组的映射
我正在使用角。以下是Typescript和HTML代码。在打字稿代码中,line this.selectedPartyCandidate = this.mappedsenatecandidates.map(_ => 0); //有问题
有错误。但是它与单维数组的工作正常:this.selectedPartyArray = this.parties.map(_ => 0); //工作
。如何解决此多维阵列问题?
错误:类型'number []'不能分配给类型'number [] []'。 键入“数字”不能分配给'number []'。
.ts文件
interface Candidate {
id?: Number,
name: string,
party: string,
preferences: Array<Number>
}
interface Party {
party: String,
preferences: Array<Number>
}
interface SenateCandidate {
id: Number,
attributes: senateCandidateAttributes
}
interface senateCandidateAttributes {
updatedAt: String
}
interface mappedSentateCandidate {
party: String
candidates: Array<String>
}
constructor() {
this.selectedPartyArray = this.parties.map(_ => 0); //working
this.selectedPartyCandidate = this.mappedSenateCandidates.map(_ => 0); //has issues
}
selectedPartyArray: number[] = [];
selectedPartyCandidate: number[][] = [];
.html文件
<div *ngFor="let party of parties; index as index">
<div class="column">
<mat-form-field appearance="fill" style="max-width: 50px">
<mat-label></mat-label>
<mat-select (selectionChange)="preferenceChange($event, party)" [(ngModel)]="selectedPartyArray[index]" name="preference">
<mat-option
*ngFor="let preference of party.preferences"
[value]="preference"
>
{{preference}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="column"></div>
<div class="column">
{{ party.party }}
</div>
</div>
<p>
<!-- Submit button -->
<button mat-raised-button (click)="submit()">Submit</button>
</p>
<div class="flex-container">
<div *ngFor="let mappedSenateCandidate of mappedSenateCandidates; index as index">
<!-- <div class="column" *ngFor="let i = 1; i <= mappedSenateCandidate.length; i++">
{{`${mappedSenateCandidate}_${i}`}}
</div> -->
<div>
<div><b>{{mappedSenateCandidate.party}}</b></div>
<div class="inner-flex-container" *ngFor="let candidate of mappedSenateCandidate.candidates; index as index1">
<mat-form-field appearance="fill" style="max-width: 50px">
<mat-select [(ngModel)]="selectedPartyCandidate[index][index1]" name="preference">
<mat-option
*ngFor="let preference of senateCandidatePrefereneList"
[value]="preference"
>{{ preference }}
</mat-option>
</mat-select>
</mat-form-field>
{{candidate}}
<div></div>
</div>
</div>
</div>
</div>
I am using angular. Below are typescript and html codes. In typescript code, line this.selectedPartyCandidate = this.mappedSenateCandidates.map(_ => 0); //has issues
has error in it. But its working fine with single dimensional array : this.selectedPartyArray = this.parties.map(_ => 0); //working
. How can i fix this multidimensional array issue?
Error: Type 'number[]' is not assignable to type 'number[][]'.
Type 'number' is not assignable to type 'number[]'.
.ts file
interface Candidate {
id?: Number,
name: string,
party: string,
preferences: Array<Number>
}
interface Party {
party: String,
preferences: Array<Number>
}
interface SenateCandidate {
id: Number,
attributes: senateCandidateAttributes
}
interface senateCandidateAttributes {
updatedAt: String
}
interface mappedSentateCandidate {
party: String
candidates: Array<String>
}
constructor() {
this.selectedPartyArray = this.parties.map(_ => 0); //working
this.selectedPartyCandidate = this.mappedSenateCandidates.map(_ => 0); //has issues
}
selectedPartyArray: number[] = [];
selectedPartyCandidate: number[][] = [];
.html file
<div *ngFor="let party of parties; index as index">
<div class="column">
<mat-form-field appearance="fill" style="max-width: 50px">
<mat-label></mat-label>
<mat-select (selectionChange)="preferenceChange($event, party)" [(ngModel)]="selectedPartyArray[index]" name="preference">
<mat-option
*ngFor="let preference of party.preferences"
[value]="preference"
>
{{preference}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="column"></div>
<div class="column">
{{ party.party }}
</div>
</div>
<p>
<!-- Submit button -->
<button mat-raised-button (click)="submit()">Submit</button>
</p>
<div class="flex-container">
<div *ngFor="let mappedSenateCandidate of mappedSenateCandidates; index as index">
<!-- <div class="column" *ngFor="let i = 1; i <= mappedSenateCandidate.length; i++">
{{`${mappedSenateCandidate}_${i}`}}
</div> -->
<div>
<div><b>{{mappedSenateCandidate.party}}</b></div>
<div class="inner-flex-container" *ngFor="let candidate of mappedSenateCandidate.candidates; index as index1">
<mat-form-field appearance="fill" style="max-width: 50px">
<mat-select [(ngModel)]="selectedPartyCandidate[index][index1]" name="preference">
<mat-option
*ngFor="let preference of senateCandidatePrefereneList"
[value]="preference"
>{{ preference }}
</mat-option>
</mat-select>
</mat-form-field>
{{candidate}}
<div></div>
</div>
</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
this.mappedsenatecandidates.map(_ =&gt; 0)
将创建数字数组,但是this.selectedPartyCandidate
期望有一系列数组,因此您无法分配一个数字数量。 要分配一个空数组如果
数组然后执行此操作:
或者,如果您确实想要一个数字数组,请更改此类型。
this.mappedSenateCandidates.map(_ => 0)
will create an array of numbers, butthis.selectedPartyCandidate
expects an array of arrays, so you can't assign an array of numbers to it. If you want to assign an array of empty arrays, do this:If you want the sub arrays to have a single 0 in it, then do this:
If you want the sub arrays to have as many 0's as the original subarray has elements, then do this:
Alternatively, if you do want a single-dimensional array of numbers, then change the type on this.selectedPartyCandidate: