[英]D3.json function to return only some of json file data
我正在使用d3.csv
讀取 csv 文件,它工作正常。 讀取 csv 文件還會使用函數重命名列:
但是,我寧願從 json 文件中讀取相同的數據。
d3.csv("myfile.csv", function(d) {
return {
location: d.identifier,
date: new Date(d.created),
amount: d.count_objects
};
}).then(function getData(rawData) {
console.log(rawData[0]);
});
d3.csv
的控制台輸出返回帶有重命名列的結果:
console:
{location: "CO", date: Wed Jan 22 2020 00:00:00 GMT-0500 (Eastern Standard Time), amount: "0"}
我只是在相同的代碼中將 csv 換成了 json 文件,希望它的讀取方式類似,但事實並非如此。
d3.json("myfile.json", function(d) {
return {
location: d.identifier,
date: new Date(d.created),
amount: d.count_objects
};
}).then(function getData(rawData) {
console.log(rawData[0]);
});
但是, d3.json
的控制台輸出返回帶有原始 json 鍵的結果:
console:
{created: "2020-01-22T00:00:00.000", identifier: "CO", count_objects: "0"}
d3.json()
需要進行哪些修改才能獲得與d3.csv()
相同的結果,例如使用重命名的鍵返回結果?
d3.json不使用行函數,因為 json 數據並不總是一個數組。 要處理數據,您可以執行以下操作:
d3.json("myfile.json").then(function (json) {
return json.map(function (d) {
return {
location: d.identifier,
date: new Date(d.created),
amount: d.count_objects
};
});
}).then(function getData(rawData) {
console.log(rawData[0]);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.