从d3.json()返回数组 [英] Returning array from d3.json()

查看:227
本文介绍了从d3.json()返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为JQUERY / d3-noob,它似乎无法弄清楚如何使这项工作:

  supdog = d3 .json(dataPath,function(jsondata){
return jsondata;
})
console.log(supdog);

提前感谢。

方案

除了你的问题描述很简单,问题似乎是你假设什么是返回什么。



函数d3 .json()是一个直接返回的异步函数(假设有一个未定义的值)。只有当从后端接收到数据时,您传递给它的回调函数才会被调用。显然上下文在这里是不同的,你的回调的返回值不会自动成为d3.json的返回值(因为这已经返回long之前)。



你想做什么可能是这样的:

  var jsondata; 
d3.json(dataPath,function(dataFromServer){
jsondata = dataFromServer;
}
console.log(jsondata);



更新1:
显然,上述示例仍然不完全正确。调用console.log )是直接在d3.json()返回之后进行的,因此,服务器可能还没有完全发送回复,因此只能在回调返回时访问数据。

  var jsondata; 

function doSomethingWithData(){
console.log(jsondata);
}

d3.json(dataPath,function(dataFromServer){
jsondata = dataFromServer;
doSomethingWithData();
}

对于一个(有点蠢,但)工作示例,请参阅: http://jsfiddle.net/GhpBt/10/



更新2:
上述示例演示了代码执行顺序,但不是真正值得最美丽的代码的价格。我自己不会使用这个全局变量并简化上面的例子:

  function doSomethingWithData(jsondata){
console.log(jsondata);
}

d3.json(dataPath,doSomethingWithData);注意doSomethingWithData是如何直接传递给d3.json的,而不是在匿名内部函数中单独调用它。 。



注意:这不是d3.js的特殊问题。基本上,异步的所有javascript函数都可能以类似的方式运行。如果他们返回的东西,它不会是传递的回调的返回值。


As a JQUERY/d3-noob, it seems I cannot figure out how to make this work:

  supdog = d3.json(dataPath, function(jsondata){
    return jsondata;
  })
  console.log(supdog);

Thanks in advance.

解决方案

Besides the fact that your problem description is very terse, the problem seems to be your assumptions about what is returning what.

The function d3.json() is an asynchronous function that directly returns (with an undefined value I assume). Only when the data is received from the backend, the callback function you passed to it will be called. Obviously the context is different here and the return value of your callback will not automatically become the return value of d3.json (as this one has returned "long" before already).

What you want to do is probably something like:

    var jsondata;
    d3.json(dataPath, function(dataFromServer) {
      jsondata = dataFromServer;
    }
    console.log(jsondata);

Update 1: Obviously, the above example is still not fully correct. The call to console.log() is made directly after the d3.json() returned. Thus, the server might not have sent the reply fully yet. Therefore, you can only access data when the callback is returned. Fixed example:

    var jsondata;

    function doSomethingWithData() {
      console.log(jsondata);
    }

    d3.json(dataPath, function(dataFromServer) {
      jsondata = dataFromServer;
      doSomethingWithData();
    }

For a (somewhat stupid, but) working example see: http://jsfiddle.net/GhpBt/10/

Update 2: The above example demonstrates in which order the code is executed but does not really deserve a price for most beautiful code. I myself would not use this "global" variable and simplify the above example to:

    function doSomethingWithData(jsondata) {
      console.log(jsondata);
    }

    d3.json(dataPath, doSomethingWithData);

Note how doSomethingWithData is directly passed to d3.json in stead of calling it separately in an anonymous inner function.

Note: This is not a particular problem of d3.js. Basically, all javascript functions that are asynchronous are likely to behave in a similar way. If they return something, it will not be the return value of the passed callback.

这篇关于从d3.json()返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆