2016-12-05 133 views
1

我绘制的图表和我想下面的JSON转换JSON格式

var jsonArray = [{ 
      "tagValueName": "PCI",  
      "tagValueId": 1, 
      "priorityAlertsCount": 20, 
      "investigationAlertsCount": 140, 
      "incidentAlertsCount": 100, 
      "otherAlertsCount": 40 
     }, { 
      "tagValueName": "ABC", 
      "tagValueId": 2, 
      "priorityAlertsCount": 100, 
      "investigationAlertsCount": 60, 
      "incidentAlertsCount": 20, 
      "otherAlertsCount": 20 
     }]; 

转换但图表需要在以下格式的数据

var jsonData = [{ 
       "tagValueName" : "PCI", 
       "type": "priorityAlertsCount", 
       "count": "20", 
      }, 
      { 
       "tagValueName" : "PCI", 
       "type": "investigationAlertsCount", 
       "count": "140", 
      }, 
      { 
       "tagValueName" : "PCI", 
       "type": "incidentAlertsCount", 
       "count": "100", 
      }, 
      { 
       "tagValueName" : "PCI", 
       "type": "otherAlertsCount", 
       "count": "40", 
      },{ 
       "tagValueName" : "ABC", 
       "type": "priorityAlertsCount", 
       "count": "100", 
      },{ 
       "tagValueName" : "ABC", 
       "type": "investigationAlertsCount", 
       "count": "60", 
      }]; 

我怎样才能将JSON从我的格式转换为图表所需的格式?
请帮助,因为我是新来的JS?

我想绘制一个图表,如this

回答

1

看看this jsbin

var jsonArray = [{ 
    "tagValueName": "PCI",  
    "tagValueId": 1, 
    "priorityAlertsCount": 20, 
    "investigationAlertsCount": 140, 
    "incidentAlertsCount": 100, 
    "otherAlertsCount": 40 
}, { 
    "tagValueName": "ABC", 
    "tagValueId": 2, 
    "priorityAlertsCount": 100, 
    "investigationAlertsCount": 60, 
    "incidentAlertsCount": 20, 
    "otherAlertsCount": 20 
}]; 

var descriptionProperties = ['tagValueName', 'tagValueId']; 
var nameProperty = ['tagValueName']; 

function formatData(dataArray) { 
    return dataArray.reduce(function(acc, item) { 
    var formattedItems = Object.keys(item) 
     .filter(function(key) { 
     return descriptionProperties.indexOf(key) === -1; 
     }) 
     .map(function(key) { 
     var obj = {}; 
     var value = item[key]; 
     obj.type = key; 
     obj.count = value; 
     obj[nameProperty] = item[nameProperty]; 
     return obj; 
     }); 
    acc = acc.concat(formattedItems); 
    return acc; 
    }, []); 
} 

console.log(formatData(jsonArray)); 

基本上你应该看看基本阵列的功能,如concat以及高阶函数mapfilterreduce我都用在了所需要的方式来转换数据。

1

您可以使用array#reduce遍历您的数组,然后使用Object.keys遍历每个对象的属性。如果密钥名称在名称末尾包含count,请将此属性添加到输出中。

var jsonArray = [{ "tagValueName": "PCI", "tagValueId": 1, "priorityAlertsCount": 20, "investigationAlertsCount": 140, "incidentAlertsCount": 100, "otherAlertsCount": 40 }, { "tagValueName": "ABC", "tagValueId": 2, "priorityAlertsCount": 100, "investigationAlertsCount":60, "incidentAlertsCount": 20, "otherAlertsCount": 20 }]; 
 

 
var result = jsonArray.reduce((r, o) => { 
 
    Object.keys(o).forEach(key => { 
 
    if(/count$/i.test(key)) { 
 
     r.push({tagValueName: o.tagValueName, type: key, count: o[key]}); 
 
    } 
 
    }) 
 
    return r; 
 
},[]); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }