繁体   English   中英

箭头函数:这个块解释了什么?

[英]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 文件保存为单独的文件。

箭头功能被用作对方法匿名回调函数.thenfilter.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM