"dynaModel": [
{
"map": {
"UNIT/SUBUNIT": "EAS",
"SUBUNIT/ISU/GEO": "Africa",
"'APR-16'_REVENUEUSD-$": "$805,298",
"'APR-16'_COSTUSD-$": "$701,026",
"'APR-16'_GMINR-RSUSD-$": 12.95,
"'Total'_REVENUEUSD-$": "$805,298",
"'Total'_COSTUSD-$": "$701,026",
"'Total'_GMINR-RSUSD-$": 12.95
}
},
{
"map": {
"UNIT/SUBUNIT": "fgdfg",
"SUBUNIT/ISU/GEO": "dfgdfg",
"'APR-16'_REVENUEUSD-$": "$58,",
"'APR-16'_COSTUSD-$": "$32,",
"'APR-16'_GMINR-RSUSD-$": 43.98,
"'Total'_REVENUEUSD-$": "$58,",
"'Total'_COSTUSD-$": "$32,",
"'Total'_GMINR-RSUSD-$": 43
}
},
.... .....
如何使用angular 2
将其转换为以下格式 output= [
{
"UNIT/SUBUNIT": "hjgh",
"SUBUNIT/ISU/GEO": "ghjg",
"'APR-16'_REVENUEUSD-$": "$805",
"'APR-16'_COSTUSD-$": "$70",
"'APR-16'_GMINR-RSUSD-$": 12.95,
"'Total'_REVENUEUSD-$": "$80",
"'Total'_COSTUSD-$": "$701",
"'Total'_GMINR-RSUSD-$": 12
},
{
"UNIT/SUBUNIT": "jjghjhj",
"SUBUNIT/ISU/GEO": "hjgh",
"'APR-16'_REVENUEUSD-$": "$58,",
"'APR-16'_COSTUSD-$": "$32,",
"'APR-16'_GMINR-RSUSD-$": 43.98,
"'Total'_REVENUEUSD-$": "$58,0",
"'Total'_COSTUSD-$": "$32,",
"'Total'_GMINR-RSUSD-$": 43.98
},
.. .. ..
];
我最近尝试如下 我使用以下代码从第一个json文件中提取键值对,然后在jsonObject数组中推送每一对。
jsonObject = [];
for(let isugeo of this.responseStatus.dynaModel){
for( let isugeomap of this.generateArray(isugeo.map)){
this.jsonObject.push(isugeomap.key+':'+"'" +isugeomap.value+"'");
} console.log('Request Completed' +
JSON.stringify(this.jsonObject));
};
generateArray(responseStatus){
return Object.keys(responseStatus).map((key) => { return {key:key, value:responseStatus[key]}});
}
但它没有给我正确的格式任何帮助我将如何进行
答案 0 :(得分:2)
我会使用js map方法(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
let output = this.responseStatus.dynaModel.map(item => item.map);
答案 1 :(得分:1)
dynaModel
是一个对象数组,因此您可以循环它并选择每个对象的map
属性(也是对象)并将该对象放入output
数组中。
let response = {
"dynaModel": [
{
"map": {
"UNIT/SUBUNIT": "EAS",
"SUBUNIT/ISU/GEO": "Africa",
"'APR-16'_REVENUEUSD-$": "$805,298",
"'APR-16'_COSTUSD-$": "$701,026",
"'APR-16'_GMINR-RSUSD-$": 12.95,
"'Total'_REVENUEUSD-$": "$805,298",
"'Total'_COSTUSD-$": "$701,026",
"'Total'_GMINR-RSUSD-$": 12.95
}
},
{
"map": {
"UNIT/SUBUNIT": "fgdfg",
"SUBUNIT/ISU/GEO": "dfgdfg",
"'APR-16'_REVENUEUSD-$": "$58,",
"'APR-16'_COSTUSD-$": "$32,",
"'APR-16'_GMINR-RSUSD-$": 43.98,
"'Total'_REVENUEUSD-$": "$58,",
"'Total'_COSTUSD-$": "$32,",
"'Total'_GMINR-RSUSD-$": 43
}
}
]
}
let output = new Array();
for (let object of response.dynaModel) {
output.push(object.map);
}
console.log(output);
打印:
[ { 'UNIT/SUBUNIT': 'EAS',
'SUBUNIT/ISU/GEO': 'Africa',
'\'APR-16\'_REVENUEUSD-$': '$805,298',
'\'APR-16\'_COSTUSD-$': '$701,026',
'\'APR-16\'_GMINR-RSUSD-$': 12.95,
'\'Total\'_REVENUEUSD-$': '$805,298',
'\'Total\'_COSTUSD-$': '$701,026',
'\'Total\'_GMINR-RSUSD-$': 12.95 },
{ 'UNIT/SUBUNIT': 'fgdfg',
'SUBUNIT/ISU/GEO': 'dfgdfg',
'\'APR-16\'_REVENUEUSD-$': '$58,',
'\'APR-16\'_COSTUSD-$': '$32,',
'\'APR-16\'_GMINR-RSUSD-$': 43.98,
'\'Total\'_REVENUEUSD-$': '$58,',
'\'Total\'_COSTUSD-$': '$32,',
'\'Total\'_GMINR-RSUSD-$': 43 } ]