【问题标题】:Convert array with json data into an array with field and value property将带有 json 数据的数组转换为带有 field 和 value 属性的数组
【发布时间】:2018-06-01 08:02:36
【问题描述】:

我已经使用这两个单独的文件将外部 json 文件中的数据提取到一个数组中。 服务文件

getProjectNames(){
return this._http.get(this._url)
.map((response:Response) => response.json())
}

组件文件

ngOnInit(){
this.roadmapGeneratorService.getProjectNames()
.subscribe(resProjectNames => this.projectNames = resProjectNames) 
}

现在我必须将projectNames 的值显示为p-dropdown element of primeng。为此,我需要将 projectNames 数组转换/复制为带有标签和值字段的数组。

for(let index=0;index<this.len;index++)
{
this.portNames.push ({label: this.projectNames[index], value: ''});
}

但它没有帮助。

【问题讨论】:

    标签: angular angular2-services primeng angular2-components


    【解决方案1】:

    试试这个:

    for(let key in this.projectNames)
    {
        this.portNames.push({label: this.projectNames[key], value: this.projectNames[key]});
    }
    

    【讨论】:

    • 这是我的 json 文件 [ { "f1": "Project1", "f2": "Project2", } ]
    • 所以根据你的回答我应该写什么来代替 label_field
    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 2020-11-20
    • 2023-03-03
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    相关资源
    最近更新 更多