简体   繁体   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}`);
        });
    });
}

My friend handed over this code for me but I'm having a hard time understanding the arrow functions.我的朋友为我移交了这段代码,但我很难理解箭头函数。 Can anyone explain what this function is performing?谁能解释一下这个功能在做什么? The ID and JSON files are saved as separate files. ID 和 JSON 文件保存为单独的文件。

The arrow functions are being used as anonymous callback functions on the methods .then , filter and .forEach .箭头功能被用作对方法匿名回调函数.thenfilter.forEach

You could just as well write it like this:你也可以这样写:

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