【发布时间】:2020-07-30 08:49:28
【问题描述】:
我正在 Angular 中制作应用程序。我有一个 Json 文件。 如何获取数组中的所有“名称”值? 假设在输出中我可以得到类似的东西:
['Sony', 'Apple', 'Sony']
json:
[
{
"Data": [
{
"name": "Sony",
"color": "black"
}
]
},
{
"Data": [
{
"name": "Apple",
"color": "white"
}
]
},
{
"Data": [
{
"name": "Sony",
"color": "red"
}
]
}
]
app.component:
import { Component, OnInit } from '@angular/core';
import resData from '../assets/result.json';
@Component({
selector: 'app-root',
templateUrl: './app.component.pug',
styleUrls: ['./app.component.styl']
})
export class AppComponent {
tasks: any = resData;
console.log(this.tasks); //While displaying just array
}
【问题讨论】:
-
您发布的 JSON 代码无效。具体来说,您在根数组中有一个带有重复键“Data”的对象。
-
@ErnestoStifano,我很抱歉。我的错。代码 UPD
标签: javascript json angular