首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从angular4中的json中获取基于元的数据

如何从angular4中的json中获取基于元的数据
EN

Stack Overflow用户
提问于 2017-11-02 19:01:25
回答 1查看 30关注 0票数 0

我试图从jsondata.json的“数据”中获取视图和度量。在这里,假设我不知道jsondata.json的‘数据’里面是什么,但我知道'meta‘,其中'viewbys’和‘措施’告诉什么是在‘数据’的jsondata.json。因此,我想从“数据”中获取所有的“viewBys”和“度量”数据。

jsondata.json

代码语言:javascript
复制
{
    "meta": {
        "viewBys": ["Month"],
        "meassures": ["sNPS", "SL%" ],
    },
    "data": [
        {
            "Month": "Jan",
            "sNPS": "58.2",
            "SL%": "90.38",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Feb",
            "sNPS": "51.2",
            "SL%": "89.48",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Mar",
            "sNPS": "53.2",
            "SL%": "86.98",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Apr",
            "sNPS": "55.8",
            "SL%": "85.82",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "May",
            "sNPS": "55.4",
            "SL%": "80.46",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Jun",
            "sNPS": "54.5",
            "SL%": "78.43",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Jul",
            "sNPS": "54.1",
            "SL%": "77.77",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Aug",
            "sNPS": "54",
            "SL%": "84.72",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Sep",
            "sNPS": "52.9",
            "SL%": "84.47",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Oct",
            "sNPS": "57.6",
            "SL%": "85.43",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Nov",
            "sNPS": "55.9",
            "SL%": "89.92",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        },
        {
            "Month": "Dec",
            "sNPS": "58.6",
            "SL%": "89.37",
            "TOS": "",
            "E2E TAT": "",
            "MPU": "",
            "Compliance": ""
        }
    ]

}

test.component.ts

代码语言:javascript
复制
ngOnInit() {
     loadtrendData ();
}
loadtrendData = (id?) => {
    this.jsonDataService.getTrendDataOverall().subscribe(
        res => this.getPartOfData(id, res)
    );
}

getPartOfData(id, jsondataaa) {
    //here, let's assume that I dont know what is inside 'data' of 
    //jsondata.json ie 'Month','sNPS' etc.. But I know what inside 'meta' of
    //jsondata.json. So want to get 'data' ie "Month" (ie 'viewbys' a/q to 'meta' of jsondata.json) and 'sNPS' ,SL% (ie 'meassures' a/q to 'meta' of jsondata.json)

    this.viewbys = jsondataaa['meta']['viewBys'];  //it gives ['Month']
    this.measures = jsondataaa['meta']['meassures']; // it gives ["sNPS", "SL%"]

    //Now, I want to get 'Month','sNPS','SL%' using 'this.viewbys' and //'this.measures' from 'data' of jsondata.json.
    // I am trying to get 'sNPS'like 

    let measure1 = jsondataaa['meta']['meassures'][0]; //ie only- sNPS
    this.measureddata1 = jsondataaa['data'].map(el => el.measure1); 
    //error and giving undefined in array.
    this.measureddata1 = jsondataaa['data'].map(el => el.sNPS);  // no error and its working

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-02 19:05:37

当你做的时候

代码语言:javascript
复制
jsondataaa['data'].map(el => el.measure1)

它正在寻找measure1属性el (它不存在)。你想让它去寻找你的动态属性。试一试

代码语言:javascript
复制
jsondataaa['data'].map(el => el[measure1])

现在它正在寻找el的动态属性(在您的例子中是sNPS)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47082788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档