1 回答

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
雖然關(guān)于目標(biāo)格式的預(yù)期用途的問題尚不清楚,但可以輕松實(shí)現(xiàn)示例的實(shí)際轉(zhuǎn)換。
如果我們假設(shè)所有輸入元素共享相同的數(shù)據(jù)屬性(Jul *在這種情況下),我們可以從任意輸入元素中提取標(biāo)簽。數(shù)據(jù)集可以通過映射輸入數(shù)據(jù)獲得。
// a filter function to determine the data properties we're interested in
let dataPropertiesFilter = (k) => k !== "flight" && k !== "range";
let result = {
// this assumes the first element has all data properties set and subsequent ones share the same properties
labels: Object.keys(input[0]).filter(dataPropertiesFilter),
// transforms each input element into the target format
datasets: input.map(e => {
return {
label: e.flight,
data: Object.keys(e)
.filter(dataPropertiesFilter)
.map(v => e[v])
};
})
};
這是一個(gè)用于演示的代碼框:https ://codesandbox.io/s/stack-overflow-q-62406854-vzxnq?file=/src/index.js
添加回答
舉報(bào)