在Javascript中将局部变量传递给匿名函数(D3过滤)

时间:2018-01-01 04:52:39

标签: javascript d3.js scope anonymous-function local-variables

我试图将一个局部变量(一个字典)传递给一个D3过滤器函数,但是这样做却收效甚微。

d3appendedElement.on("click", function(d){

    var county_list = state_retriever[d.id];    
    //county_list is a dictionary: 
    //county_list = {"56001": true, "56003": true, "56005": true,...}

    embedGroup.filter(function(e){
        console.log(e); 
        //e = ["16079", -0.36402252840408394, -0.5303157467831482]
        //here, I would like to have access to county_list. However, I do not.

        //The following does not work; I get
        // `Uncaught ReferenceError: county_list is not defined`
        return e[0] in county_list;
    });
}

county_list是与true匹配的字符串字典。 embedGroup是d3对象,我尝试根据它是否在["16079", -0.36, -0.53]字典中过滤每个数据点( 16079 上的过滤器county_list)。

有没有办法在不将county_list作为全局变量的情况下执行此操作?

0 个答案:

没有答案