我想从数组中检索值,我的代码就像
this.RoleServiceService.getRoleById(this.id).subscribe(data => {
this.roleData.push(data['data']);
console.log(this.roleData);
})
但是我得到像这样的数组
我试过像role=roleData[0];
但是给你undefined可以帮助我
[]
0
:
Array(4)
0
:
{id: 5, name: "edit_page", guard_name: "api", created_at: "2018-03-30 10:09:38", updated_at: "2018-03-30 10:09:38", …}
1
:
{id: 6, name: "create_page", guard_name: "api", created_at: "2018-03-30 10:09:38", updated_at: "2018-03-30 10:09:38", …}
2
:
{id: 7, name: "create_post", guard_name: "api", created_at: "2018-04-06 11:11:40", updated_at: "2018-04-06 11:11:40", …}
3
:
{id: 8, name: "view_post", guard_name: "api", created_at: "2018-04-06 11:11:40", updated_at: "2018-04-06 11:11:40", …}
length
:
4
答案 0 :(得分:1)
你必须采取this.roleData = data['data']
由于data['data']
会返回array
,因此将该数组推送到第一个index
是错误的;
this.roleData = [];
this.RoleServiceService.getRoleById(this.id).subscribe(data => {
this.roleData = data['data'];
console.log(data['data']);
console.log(this.roleData);
})
如果您想append the data
,还可以使用for loop
提供数据
this.RoleServiceService.getRoleById(this.id).subscribe(data => {
data['data'].forEach(element => {
this.roleData.push(element)
});
console.log(data['data']);
console.log(this.roleData);
})