如何将一个 [object Object] 变成一个数组?
How to turn an [object Object] into an array?
我正在显示来自 API 调用 ngFor 指令的一些值。我正在尝试将数据转换为数组。这是我从服务器得到的:
Data from the server
我是这样订阅数据的:
onVideoClick(video_id) {
this._dashboardService.getVideoData(video_id)
.subscribe(videoData => {
if (videoData.ok) {
console.log(videoData.json());
this.videoData = videoData.json();
} else {
console.log('pas de données vidéos');
}
});
}
我这样显示我的数据:
<tr>
<th *ngFor="let vD of videoData">{{vD.attr_libelle}}</th>
</tr>
如何把这个对象变成数组?
我将使用库 Lodash 进行此转换。
onVideoClick(video_id) {
this._dashboardService.getVideoData(video_id)
.subscribe(videoData => {
if (videoData.ok) {
console.log(videoData.json());
this.videoData = _.values(videoData.json());
} else {
console.log('pas de données vidéos');
}
});
}
有关文档,请参阅 here。
您可以为此实现自定义管道。这是一个从对象条目创建数组的示例:
@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
let keys = [];
for (let key in value) {
keys.push({key: key, value: value[key]);
}
return keys;
}
}
并像这样使用它:
<span *ngFor="#entry of content">
Key: {{entry.key}}, value: {{entry.value}}
</span>
不要忘记将管道添加到要使用它的组件的 pipes
属性中。
我正在显示来自 API 调用 ngFor 指令的一些值。我正在尝试将数据转换为数组。这是我从服务器得到的:
Data from the server
我是这样订阅数据的:
onVideoClick(video_id) {
this._dashboardService.getVideoData(video_id)
.subscribe(videoData => {
if (videoData.ok) {
console.log(videoData.json());
this.videoData = videoData.json();
} else {
console.log('pas de données vidéos');
}
});
}
我这样显示我的数据:
<tr>
<th *ngFor="let vD of videoData">{{vD.attr_libelle}}</th>
</tr>
如何把这个对象变成数组?
我将使用库 Lodash 进行此转换。
onVideoClick(video_id) {
this._dashboardService.getVideoData(video_id)
.subscribe(videoData => {
if (videoData.ok) {
console.log(videoData.json());
this.videoData = _.values(videoData.json());
} else {
console.log('pas de données vidéos');
}
});
}
有关文档,请参阅 here。
您可以为此实现自定义管道。这是一个从对象条目创建数组的示例:
@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
let keys = [];
for (let key in value) {
keys.push({key: key, value: value[key]);
}
return keys;
}
}
并像这样使用它:
<span *ngFor="#entry of content">
Key: {{entry.key}}, value: {{entry.value}}
</span>
不要忘记将管道添加到要使用它的组件的 pipes
属性中。