Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从类型类数组的数组访问数据7_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 从类型类数组的数组访问数据7

Javascript 从类型类数组的数组访问数据7,javascript,angular,typescript,Javascript,Angular,Typescript,我已经声明了一个subject[]类型的数组,并从一个api获取信息,然后将subject推送到Arr数组中。 我的控制台看起来像附加的图像。 我想使用angular访问主题,当我尝试使用它时。主题[0]返回undefined 所以它不是数组中的数组,我需要一些帮助来访问数据,我将使用它在图表js的图形上动态创建标签 Arr: Subject [] = new Array(); loadSubjects() { this.ss.getSubjects().subscribe(( subj

我已经声明了一个subject[]类型的数组,并从一个api获取信息,然后将subject推送到Arr数组中。 我的控制台看起来像附加的图像。 我想使用angular访问主题,当我尝试使用它时。主题[0]返回undefined

所以它不是数组中的数组,我需要一些帮助来访问数据,我将使用它在图表js的图形上动态创建标签

Arr: Subject [] = new Array();
 
loadSubjects() {
  this.ss.getSubjects().subscribe(( subject: Subject[]) => {
    this.subject = subject;
    for (let i = 0; i < this.subject.length; i++) {
        this.subjArray.push(this.subject[i]['name']);
    }
  });
}
Arr:Subject[]=新数组();
加载主题(){
this.ss.getSubjects().subscribe((主题:subject[])=>{
this.subject=主语;
for(设i=0;i
这可以使用映射功能完成

对于Plunker解决方案

同时添加以下代码:

this.list1 = [
  {Name: "abc", Capacity: "150", Series: "20", Make: "150", Model: "150", TowerHeight: "151"},
  {Name: "def", Capacity: "250", Series: "250", Make: "250", Model: "252", TowerHeight: "250"},
  {Name: "ghi", Capacity: "151", Series: "21", Make: "151", Model: "151", TowerHeight: "152"}
  ];


this.list2 = this.list1.map(item => { return item.Name});
对你来说,我想会是这样的

this.subjArray = this.subject.map(item => { return item.name});
输出 列表1:

[
    {
        "Name": "abc",
        "Capacity": "150",
        "Series": "20",
        "Make": "150",
        "Model": "150",
        "TowerHeight": "151"
    },
    {
        "Name": "def",
        "Capacity": "250",
        "Series": "250",
        "Make": "250",
        "Model": "252",
        "TowerHeight": "250"
    },
    {
        "Name": "ghi",
        "Capacity": "151",
        "Series": "21",
        "Make": "151",
        "Model": "151",
        "TowerHeight": "152"
    }
]
[ "abc", "def", "ghi" ]
列表2:

[
    {
        "Name": "abc",
        "Capacity": "150",
        "Series": "20",
        "Make": "150",
        "Model": "150",
        "TowerHeight": "151"
    },
    {
        "Name": "def",
        "Capacity": "250",
        "Series": "250",
        "Make": "250",
        "Model": "252",
        "TowerHeight": "250"
    },
    {
        "Name": "ghi",
        "Capacity": "151",
        "Series": "21",
        "Make": "151",
        "Model": "151",
        "TowerHeight": "152"
    }
]
[ "abc", "def", "ghi" ]

这也可以通过for循环实现。但对于需要在数组中获取单个属性值的图表数据场景,map将更简单,这可以使用map函数完成

对于Plunker解决方案

同时添加以下代码:

this.list1 = [
  {Name: "abc", Capacity: "150", Series: "20", Make: "150", Model: "150", TowerHeight: "151"},
  {Name: "def", Capacity: "250", Series: "250", Make: "250", Model: "252", TowerHeight: "250"},
  {Name: "ghi", Capacity: "151", Series: "21", Make: "151", Model: "151", TowerHeight: "152"}
  ];


this.list2 = this.list1.map(item => { return item.Name});
对你来说,我想会是这样的

this.subjArray = this.subject.map(item => { return item.name});
输出 列表1:

[
    {
        "Name": "abc",
        "Capacity": "150",
        "Series": "20",
        "Make": "150",
        "Model": "150",
        "TowerHeight": "151"
    },
    {
        "Name": "def",
        "Capacity": "250",
        "Series": "250",
        "Make": "250",
        "Model": "252",
        "TowerHeight": "250"
    },
    {
        "Name": "ghi",
        "Capacity": "151",
        "Series": "21",
        "Make": "151",
        "Model": "151",
        "TowerHeight": "152"
    }
]
[ "abc", "def", "ghi" ]
列表2:

[
    {
        "Name": "abc",
        "Capacity": "150",
        "Series": "20",
        "Make": "150",
        "Model": "150",
        "TowerHeight": "151"
    },
    {
        "Name": "def",
        "Capacity": "250",
        "Series": "250",
        "Make": "250",
        "Model": "252",
        "TowerHeight": "250"
    },
    {
        "Name": "ghi",
        "Capacity": "151",
        "Series": "21",
        "Make": "151",
        "Model": "151",
        "TowerHeight": "152"
    }
]
[ "abc", "def", "ghi" ]
这也可以通过for循环实现。但对于需要在数组中获取单个属性值的图表数据场景,在角度图中映射会更简单