angular - 显示数据时未定义
问题描述
结果,当我想显示数据时,我得到undefined。
这只发生在进度数组中。
其余数据不存在这样的问题。
为什么会发生这种情况,我该如何解决?
console.log(this.splineChart)
[{…}]
0:
data: Array(1)
0: Array(2)
0: undefined
1: undefined
ts:
user: User[];
splineChart = [];
load() {
this.route.params
.pipe(
switchMap(
(params: Params) => {
if (params['id']) {
return this._peopleService.getPeopleById(params['id'])
}
return of(null)
}
)
)
.subscribe(
(user: User[]) => {
this.splineChart = [{
data: user.map(function (item) {
return [item.progress.tasks, item.progress.week];
})}]
},
error => {
console.log(error)
}
)
}
我的 json 文件::
{
"users": [
{
"id": 1,
"fname": "Tommie",
"lname": "Shurtleff",
"photo": "tommie",
"money": "667.16",
"progress": [
{
"id": 1,
"week": 1,
"tasks": 29
},
...
]
}
]
}
解决方案
Progress 是一个数组,您将其视为对象。item.progress.tasks
这不适用于数组。
你必须循环item.progress
获得期望的结果。
将您的订阅部分替换为以下内容。
this.splineChart = [{
data: user.map(function(item) {
return item.progress.map(progress => [progress.tasks, progress.week]);
})
}];
请参阅下面的示例。
const user = [{
"id": 1,
"fname": "Tommie",
"lname": "Shurtleff",
"photo": "tommie",
"money": "667.16",
"progress": [{
"id": 1,
"week": 1,
"tasks": 29
},
{
"id": 1,
"week": 2,
"tasks": 30
},
]
}];
let splineChart;
this.splineChart = [{
data: user.map(function(item) {
return item.progress.map(progress => [progress.tasks, progress.week]);
})
}];
console.log(this.splineChart)
推荐阅读
- c - 获取结构内变量的偏移量是基于 NULL 指针,但为什么呢?
- facebook-graph-api - 当所有元数据都正常时,预览 url 显示不正确
- python - 如何遍历文件夹中的文件以移动具有特定扩展名的每个文件
- sql - 根据从第一个开始的日期加入两个表
- react-native - ReactNative 导航问题
- reactjs - Spotify API 帮助。response_type=code 不工作, response_type=token 可以吗?
- r - 在给出一个带有串扰和情节组合的代码块之后。我无法正确导出任何 html 输出
- c# - 我怎么知道哪个聊天在电报机器人上生成“找不到聊天”异常?
- python - 从 redis 数据存储中使用 python huey 获取任务结果
- magento - 不正确的 Magento 2 布局覆盖行为