Javascript 为什么阵列在角度投影中只带来一个对象?
我正在使用传奇联盟API,更具体地说是他们带来的冠军json 我用Angular提供了这项服务:Javascript 为什么阵列在角度投影中只带来一个对象?,javascript,arrays,json,angular,Javascript,Arrays,Json,Angular,我正在使用传奇联盟API,更具体地说是他们带来的冠军json 我用Angular提供了这项服务: import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders} from '@angular/common/http'; const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) } @In
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
}
@Injectable({
providedIn: 'root'
})
export class ChampionsService {
constructor(private http: HttpClient) { }
getChampions(){
return this.http.get('http://ddragon.leagueoflegends.com/cdn/10.23.1/data/es_ES/champion.json');
}
}
这是我的.ts文件:
import { Component, OnInit } from '@angular/core';
import {ChampionsService } from '../../services/champions.service';
@Component({
selector: 'app-champions',
templateUrl: './champions.component.html',
styleUrls: ['./champions.component.css']
})
export class ChampionsComponent implements OnInit {
public champions;
public arrayChampions;
constructor(private championsService:ChampionsService) { }
ngOnInit(): void {
this.getAllChampions();
}
getAllChampions(){
this.championsService.getChampions().subscribe(
data => { this.champions = data,
this.arrayChampions = Object.entries(this.champions.data).map(([k,v]) => ({ [k]:v })),
this.ArrayIterator();
},
err => {console.error(err)},
() => console.log("Champions cargados")
);
}
ArrayIterator() {
let IteratableArray = Array();
for (let item of Object.keys(this.arrayChampions[0])) {
var eventItem = Object.values(this.arrayChampions[0]);
IteratableArray.push(eventItem);
}
this.arrayChampions = IteratableArray[0];
}
}
这是html:
<p>champions works!</p>
{{arrayChampions | json}}
<!-- Cards -->
<div *ngFor="let arrayChampion of arrayChampions" class="card mb-3">
<div class="card-header">
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
<a class="text-decoration-none">{{arrayChampion.id}}</a>
</blockquote>
</div>
<div class="card-footer">
</div>
</div>
冠军作品
{{arrayChampions | json}}
{{arrayChampion.id}
正如你所看到的,var“arrayChampions”只带来了第一个冠军(Atrox),而据我所知,它应该带来所有冠军(我对javascript和Angular都是新手)。根据你的exmaple,我在这里创建并堆叠了Blitz,它使整个
arrayChampions
迭代了它的值
请找到正在工作的Blitz
示例HTML:
<hello name="{{ name }}"></hello>
<!-- {{this.products |json}} -->
<ul>
<li *ngFor="let champ of products | keyvalue">
<label style="font-size: 20px;font-weight: bold;color: red;">
{{champ.key}}
</label>
<ul *ngFor="let item of champ.value | keyvalue">
<li>
{{item.key}} : {{item.value}}
<ul *ngFor="let sub of item.value | keyvalue">
<li>
{{sub.key}} : {{sub.value}}
</li>
</ul>
</li>
</ul>
</li>
</ul>
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { map, catchError, tap } from "rxjs/operators";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
apiURL: string =
"https://ddragon.leagueoflegends.com/cdn/10.23.1/data/es_ES/champion.json";
name = "Angular";
products = [];
constructor(private httpClient: HttpClient) {}
ngOnInit() {
this.getChamp();
}
getChamp() {
this.httpClient.get(this.apiURL).subscribe((data: any) => {
this.products = data.data;
Object.keys(data.data).map((key: any, obj: any) => obj[key]);
});
}
}
我想你的ArrayIterator有问题,我在stackblizt here=>中做了一个简单的测试,它的工作很好,谢谢你。我应用了你的代码,它工作了。你能告诉我要理解的具体解释吗?当然。。这行中发生了什么
Object.keys(data.data).map((key:any,obj:any)=>obj[key])代码>产生魔力。它从API调用中获取heros响应,并迭代每个对象,通过其键获取每个对象的值,并将其分配到模板可以迭代的另一个数组中。这就是那一行发生的事情。如果可能的话,我非常感谢在两个答案中投赞成票。很乐意帮忙