简体   繁体   English

访问第一个对象及其内部的数组

[英]Accessing first Object and an arrays inside it

I know im not suppose to use map because because i cant iterate through an object. 我知道我不应该使用地图,因为因为我不能遍历一个对象。 i just have no idea what to use to go through the fist object aod. 我只是不知道要用什么来检查拳头物体。

 ngOnInit() { this.chartsService.getAOD().subscribe(data => { (this.data as any) = data; console.log(this.data); this.data.map(values => { console.log(values); }); }); this.chartOptions(); } 

在此处输入图片说明

Three static methods on Object provide iterables from objects 对象上的三种静态方法提供对象的可迭代性

ngOnInit() {
  this.chartsService.getAOD().subscribe(data => {
    (this.data as any) = data;
    Object.entries(this.data).map(([key, value]) => console.log(key, value));
    Object.keys(this.data).map((key) => console.log(key));
    Object.values(this.data).map((value) => console.log(value));
  });
  this.chartOptions();
}

To iterate over the aod field you have to do the following: 要遍历aod字段,您必须执行以下操作:

ngOnInit() {
   this.chartsService.getAOD().subscribe(data => {
     (this.data as any) = data;
     console.log(this.data);
     this.data.aod.forEach(values => {
       console.log(values);
     });
  });
this.chartOptions();
}

In typescript for access to a public field inside an object you just have to type object.fieldname, and for iterate over an Array, you can use forEach function. 在用于访问对象内部公共字段的typescript中,您只需键入object.fieldname,并遍历Array,就可以使用forEach函数。

Good luck. 祝好运。

The javascript .map method only available for Array but current this.data variable data type as object. javascript .map方法仅适用于Array,但当前this.data变量数据类型为对象。 You can convert Object into Arra Or use ForEach or For loop 您可以将对象转换为Arra或使用ForEach或For循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM