现在我可以通过使用nodejs将postgres数据库作为json输出查询来获取数据,如下所示
[{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","alarmaction","actionexecutionmode","cost","isparent__"],"values":["100","test_bsc_interface","test_bsc_interface","test","Open Loop","regex","HPSA",null,null,"Asynchronous",null,"0"]},{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","alarmaction","actionexecutionmode","cost","isparent__"],"values":["101","check_tt","check_tt","test","Open Loop","None","Trouble Ticket","check_tt",null,"Synchronous",null,"0"]}
我期待输出,其中应该避免重复的标题。这是什么方法?下面是我尝试的代码片段仍然无法正常工作,我也使用_.each。请任何人告诉我解决方案
var result = _.map(json_object, function(o) {
return {headers:keys[0], values : _.values(o)}
});
[{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","","actionexecutionmode","cost","isparent__"],"values":["100","test_bsc_interfacalarmactione","test_bsc_interface","test","Open Loop","regex","HPSA",null,null,"Asynchronous",null,"0"]},,"values":["101","check_tt","check_tt","test","Open Loop","None","Trouble Ticket","check_tt",null,"Synchronous",null,"0"]}
答案 0 :(得分:0)
我认为你的问题来自我回答的另一篇文章:
How to format the JSON object key/value pair
var _ = require('lodash');
var data = [
{ id: '714', actionname: 'test_bsc_interface', actionid: '100' },
{ id: '715', actionname: 'list_all_available_interfaces', actionid: '103' },
{ id: '716', actionname: 'check_tt', actionid: '101' }
];
var result;
if (_.size(data) > 0) {
result = {
headers: _.keys(data[0]),
values: _.map(data, function(o) {
return _.values(o);
})
};
}
console.dir(result);
,输出为:
{
headers: [ 'id', 'actionname', 'actionid' ],
values: [
[ '714', 'test_bsc_interface', '100' ],
[ '715', 'list_all_available_interfaces', '103' ],
[ '716', 'check_tt', '101' ]
]
}