gpt4 book ai didi

javascript - 带有dimple.js图表​​的嵌套JSON对象数据

转载 作者:行者123 更新时间:2023-12-03 06:38:55 27 4
gpt4 key购买 nike

我需要帮助来展平嵌套格式的 json 数据。我尝试了一些在 S/O 中找到的东西,但没有多大帮助。我花了很多时间用静态数据创建图表,我不知道我会以这种格式获取数据。任何帮助都感激不尽!我正在使用凹坑垂直堆栈变量

 {
"head": {
"vars": [ "Country" , "City" , "County" , "Fiscal_Year" , "Fiscal_Qtr" , "Start_Period" , "End_Period" , "Extract_Date" , "AA" , "BB" , "CC" , "DD" , "EE" , "FF" , "GG" , "HH" , "II" , "JJ" , "KK" , "LL" , "MM" , "NN" ]
} ,
"results": {
"bindings": [
{
"Country": { "type": "typed-literal" , "value": "United States" } ,
"City": { "type": "typed-literal" , "value": "New York" } ,
"County": { "type": "typed-literal" , "value": "Manhattan" } ,
"Fiscal_Year": { "type": "typed-literal" , "value": "2014" } ,
"Fiscal_Qtr": { "type": "typed-literal" , "value": "1" } ,
"Start_Period": { "type": "typed-literal" , "value": "2014-10-01" } ,
"End_Period": { "type": "typed-literal" , "value": "2014-12-31" } ,
"Extract_Date": {"type": "typed-literal" , "value": "2015-01-01" } ,
"AA": { "type": "typed-literal" , "value": "0.549" } ,
"BB": { "type": "typed-literal" , "value": "0.1526" } ,
"CC": { "type": "typed-literal" , "value": "0.0258" } ,
"DD": { "type": "typed-literal" , "value": "0.047400000000000005" } ,
"EE": { "type": "typed-literal" , "value": "0.21780000000000002" } ,
"FF": {"type": "typed-literal" , "value": "0.0074" } ,
"GG": { "type": "typed-literal" , "value": "0.615" } ,
"HH": { "type": "typed-literal" , "value": "0.507" } ,
"II": { "type": "literal" , "value": "--" } ,
"JJ": {"type": "typed-literal" , "value": "0.113" } ,
"KK": { "type": "typed-literal" , "value": "0.026" } ,
"LL": { "type": "typed-literal" , "value": "0.034" } ,
"MM": { "type": "typed-literal" , "value": "0.318" } ,
"NN": { "type": "typed-literal" , "value": "0.002" }
}

我正在寻找类似这样的输出:

    [
{"Country": "United States"},
{"City": "New York"},
{"County": "Manhattan"},
{"Fiscal_Year": "2014"},
{"Start_Period": "2014-10-01"},
{"End_Period": "2014-12-32"},
{"Extract_Date": "2015-01-01" },
{"AA": "0.549" },
{"BB": "0.1526" },
{"CC": "0.0258" },
{"DD": "0.047400000000000005" },
{"EE": "0.21780000000000002" },
{"FF": "0.0074" },
{"GG": "0.615" },
{"HH": "0.507" },
{"II": "--" },
{"JJ": "0.113" },
{"KK": "0.026" },
{"LL": "0.034" },
{"MM": "0.318" },
{"NN": "0.002" }
]

最佳答案

Javascript 的 Object.keys() 接受一个对象(例如 {foo: 1, bar: 2})并返回该对象的键的数组 [ “foo”,“bar”]

使用它,您可以像这样转换对象:

data.results.bindings.map(function(original) {
var converted = {};
Object.keys(original)
.forEach(function(key) {
converted[key] = original[key].value;
});
return converted;
})

或者,使用reduce也可以实现同样的效果。

data.results.bindings.map(function(original) {
return Object.keys(original)
.reduce(function(converted, key) {
converted[key] = original[key].value;
return converted;
}, {});
})

一种方式比另一种方式没有更多的好处,除了第二个选项不依赖于定义var conversion,使得更多“内联”,这使得它更具可读性(至少在某些圈子里)。

关于javascript - 带有dimple.js图表​​的嵌套JSON对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38057382/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com