如何从angular4中的json基于'meta'获取'数据'

时间:2017-11-02 19:01:26

标签: json angular

我正在尝试从jsondata.json的'data'获取viewbys和度量。 在这里,假设我不知道jsondata.json的'data'内部是什么,但我知道'meta',其中'viewbys'和'meassures'告诉jsondata.json里面'data'的内容。所以,我想从'数据'中获取所有'viewBys'和'meassures'数据。

jsondata.json

{
    "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

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

}

1 个答案:

答案 0 :(得分:0)

当你这样做时

jsondataaa['data'].map(el => el.measure1)

正在查找measure1的{​​{1}}属性(不存在)。您希望它查找您的动态属性。尝试

el

现在它正在寻找jsondataaa['data'].map(el => el[measure1]) 的动态属性(在您的情况下为sNPS)