我想更改此对象结构
[
{
"tahun": "2010",
"apel": 100,
"pisang": 200,
"anggur": 300,
"nanas": 400,
"melon": 500
},
{
"tahun": "2011",
"apel": 145,
"pisang": 167,
"anggur": 210,
"nanas": 110,
"melon": 78
}
[
进入我的Django高清图的数组结构,
[
["2010",100],
["2010",200],
["2010",300],
["2010",400],
["2010",500],
["2011",145],
["2011",167],
["2011",210],
["2011",110],
["2011",78]
]
或者,如果你有任何方法,比如使用AJAX,那将非常有帮助
答案 0 :(得分:0)
您可以使用以下结构将原始结构缩减为所需的结构:
const data = [
{
"tahun": "2010",
"apel": 100,
"pisang": 200,
"anggur": 300,
"nanas": 400,
"melon": 500
},
{
"tahun": "2011",
"apel": 145,
"pisang": 167,
"anggur": 210,
"nanas": 110,
"melon": 78
}
];
const result = data.reduce((prev, curr) => {
const year = curr.tahun;
delete curr.tahun;
return [...prev, ...Object.keys(curr).map(key => [year, curr[key]])];
}, []);
不是最漂亮但是嘿,它有效。
答案 1 :(得分:0)
如果要将字典列表转换为服务器端的列表列表,可以这样做:
>>> data = [{'tahun': '2010', 'apel': 100, 'pisang': 200, 'anggur': 300, 'nanas': 400, 'melon': 500}, {'tahun': '2011', 'apel': 145, 'pisang': 167, 'anggur': 210, 'nanas': 110, 'melon': 78}]
>>> [[x['tahun'], x['apel']] for x in data]
[['2010', 100], ['2011', 145]]