enter image description here enter image description here
我声明了一个subject []类型的数组,并从api获取信息,然后将subject推入Arr数组。我的控制台看起来像附件中的图片。我想使用angular访问主题,当我尝试使用this.subject时,它返回未定义。
所以它不是数组中的数组,我需要一些帮助来访问这些数据,我将使用它来从图表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']);
}
});
}发布于 2020-07-10 03:30:18
这可以使用map函数来完成
柱塞解决方案的Click here
还添加了下面的代码:
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"
}
]列表2:
[ "abc", "def", "ghi" ]这也可以使用for循环来完成。但是,对于需要在数组中获取单个属性值的图表数据场景,在angular中映射会更简单
https://stackoverflow.com/questions/62822030
复制相似问题