![](/img/trans.png)
[英]How to read file using FileReader.readAsText synchronously in JavaScript?
[英]How to read a csv file synchronously and return a value from it using javascript
我通過一個名為findAbbreviation(fullName)的函數傳遞一個完整的州名。我使用全名的csv文件,縮寫對來返回與全名匹配的縮寫值。 考慮到我希望讀取csv是同步的,最好的方法是什么?
d3.csv(“candy.csv”,function(csv){
//reformat the locations to have a consistent format
//every entry in q5 is changed to an abbreviation of 2 letters
//the abbreviation lists are in the folder named such
var updatedAbbr = csv.map(function(d) {
//check to see the entry has a value
if (d.Q5_STATE != null) {
//the country is the united States
if (d.Q4_COUNTRY == "United States") {
//if the state value is longer than two characters, set it to UU
if (d.Q5_STATE.length > 2) {
d.Q5_STATE = findAbbreviation(d.Q5_STATE);
}
//pass through and check that the new value is not null
if (d.Q5_STATE != null) {
console.log(d.Q5_STATE);
}
}
}
});
});
function findAbbreviation(fullName){
}
我想傳遞價值阿拉巴馬州並得到AL。 或加利福尼亞州 - > CA.
d3.csv
是異步的,你無法做任何事情。
您可以使用XMLHttpRequest
發出同步請求( 危險 :不推薦使用此功能),然后通過d3.csv.parse
提供結果。
我建議改為使用異步編程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.