[英]Arrow functions: What is this block explaining?
function readJson(sample) {
d3.json("samples.json").then((data) => {
var extract = data.metadata
var emptyArray = extract.filter(object => object.id == sample);
var finalArray = emptyArray[0];
var Visual = d3.select("#sample-metadata");
Object.entries(finalArray).forEach(([key, value]) => {
Visual.append("h6").text(`${key} : ${value}`);
});
});
}
我的朋友為我移交了這段代碼,但我很難理解箭頭函數。 誰能解釋一下這個功能在做什么? ID 和 JSON 文件保存為單獨的文件。
箭頭功能被用作對方法匿名回調函數.then
, filter
和.forEach
。
你也可以這樣寫:
function readJson(sample) {
// .json returns a promise which resolves to a json object assigned the name data
// .then allows you to chain asynchronous functions with other async or synced functions
d3.json("samples.json").then(function(data){
var extract = data.metadata
// Filter all the objects which satisfy the provided testing function
var emptyArray = extract.filter(function(object){
return object.id == sample
});
var finalArray = emptyArray[0];
var Visual = d3.select("#sample-metadata");
// For each key/value pair in finalArray execute the provided function
Object.entries(finalArray).forEach(function([key, value]){
Visual.append("h6").text(`${key} : ${value}`);
});
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.