简体   繁体   English

在D3 Javascript中将字符串转换为数组名称

[英]Turn string to array name in D3 Javascript

I want to enter the name of the data array as a variable, but for some reason it's not working for me. 我想输入数据数组的名称作为变量,但是由于某种原因,它对我不起作用。

so in the case it works: 所以在这种情况下有效:

 d3.select('#energychart').html("");

 d3.select('#energychart')
        .selectAll('svg')
        .data([week3])
        .enter()
        .append('svg')

But this case, it doesn't 但是这种情况并没有

     var x = "week3";
     d3.select('#energychart')
        .selectAll('svg')
        .data([x])
        .enter()
        .append('svg')

If I understand correctly you are trying to get the variable with name week3 by it's string name like in this question. 如果我正确理解,您正在尝试使用名称为week3的变量作为其字符串名称,例如此问题。 If this is correct it has nothing to do with d3 as far as I know. 据我所知,如果这是正确的,则与d3无关。

I would suggest this. 我建议这个。 (where variable with name "week3" is a pre-defined global variable.) (其中名称为“ week3”的变量是预定义的全局变量。)

var varName = "week3";
var x = window[varName];
d3.select('#energychart')
  .selectAll('svg')
    .data([x])
  .enter()
    .append('svg');

If week3 is not a global variable then you will have to set up a variables object like in this answer . 如果week3不是全局变量,则必须像此答案中那样设置一个变量对象。 Then you will have to replace the window object (which holds global variables) with the variables obj. 然后,您将必须用变量obj替换窗口对象(包含全局变量)。

var variables = {
    "week3": [3,4,5,6,7] // array can be replace with any data you want as value
};

var varName = "week3";
var x = variables[varName];
d3.select('#energychart')
  .selectAll('svg')
    .data([x])
  .enter()
    .append('svg')

EDIT: 编辑:

So as requested in the comments. 因此,按照评论的要求。 Here is an explanation to how this works. 这是有关其工作原理的说明。

First thing you need to know is that window is just an object. 您需要知道的第一件事是窗口只是一个对象。 The window object holds global variables. 窗口对象保存全局变量。 Running these two lines below in your browser console will show you that all global variables are stored on the window object. 在浏览器控制台中运行以下两行,将显示所有全局变量都存储在window对象上。

var AADataSet = [1, 2, 3, 4, 5];

console.log(window);

There are different ways to get value from an object. 有多种从对象中获取价值的方法。 I will show you three equivalent ways to get a property out of an object. 我将向您展示三种从对象中获取属性的等效方法。

var result;
var obj = { foo: 1, Bar: 2, "foo bar": 3 };
var randomVarName = "Bar"; // notice the capital B in Bar is important since it was declared that way.

result = obj.foo; // result equals 1
result = obj[randomVarName]; // result equals 2
result = obj["foo bar"]; // result equals 3

Since window is an object and the variables are stored directly on the object and not nested inside. 由于window是对象,因此变量直接存储在该对象上,而不是嵌套在其中。 You can access AADataSet through window["AADataSet"] the string "AADataSet" could also be stored in a variable and then used eg 您可以通过window["AADataSet"]访问AADataSet,字符串“ AADataSet”也可以存储在变量中,然后使用,例如

var varName = "AADataSet";
window[varName]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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