gpt4 book ai didi

javascript - 从 Angular 5 的数组中获取值

转载 作者:行者123 更新时间:2023-12-03 02:07:13 25 4
gpt4 key购买 nike

我想从数组中检索值,我的代码如下

this.RoleServiceService.getRoleById(this.id).subscribe(data => {
this.roleData.push(data['data']);
console.log(this.roleData);
})

但我得到这样的数组

我已经尝试过 role=roleData[0]; 但给出了 undefined 你能帮我解决这个问题吗 enter image description here

    []
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

最佳答案

你必须接受,this.roleData = data['data']

由于data['data']返回一个数组,因此您将该数组推送到第一个index是错误的;

this.roleData = [];

this.RoleServiceService.getRoleById(this.id).subscribe(data => {
this.roleData = data['data'];
console.log(data['data']);
console.log(this.roleData);
})

如果您想附加数据,您还可以使用for循环

应用数据:

this.RoleServiceService.getRoleById(this.id).subscribe(data => {
data['data'].forEach(element => {
this.roleData.push(element)
});
console.log(data['data']);
console.log(this.roleData);
})

关于javascript - 从 Angular 5 的数组中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49753003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com