最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get values from array in angular 5 - Stack Overflow

programmeradmin4浏览0评论

i want to retrieve values from array my code is like

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

but i am getting array like this

i have tried like role=roleData[0]; but giving undefined can you please help me with this

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

i want to retrieve values from array my code is like

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

but i am getting array like this

i have tried like role=roleData[0]; but giving undefined can you please help me with this

    []
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
Share edited Apr 10, 2018 at 12:15 Pranav Mandlik asked Apr 10, 2018 at 11:55 Pranav MandlikPranav Mandlik 6446 gold badges19 silver badges45 bronze badges 22
  • you want to acess from which array? – Sajeetharan Commented Apr 10, 2018 at 11:57
  • try console.log(this.roleData[0]) – AlexFF1 Commented Apr 10, 2018 at 12:00
  • i wan to access from roleData – Pranav Mandlik Commented Apr 10, 2018 at 12:02
  • @AlexFF1 tried but giving undefined – Pranav Mandlik Commented Apr 10, 2018 at 12:02
  • what is data['data']? This looks illogical – WasiF Commented Apr 10, 2018 at 12:03
 |  Show 17 more ments

2 Answers 2

Reset to default 2

You have to take, this.roleData = data['data']

Since data['data'] returns an array, it is wrong that you are pushing that array to first index;

this.roleData = [];

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

If you want to append the data you can also use a for loop

Appeding data:

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

this.roleData = data['data'] it's worked form me..maybe the ine from someone's treatment

发布评论

评论列表(0)

  1. 暂无评论