I started working with JavaScript last week in order to create some D3 visualizations, and have become rather stuck on what can only be a very simple task.
我上周开始使用JavaScript来创建一些D3可视化,并且已经变得非常困难于只能是一个非常简单的任务。
I have various data series for different countries, each stored in arrays, e.g.
我有不同国家的各种数据系列,每个数据都存储在数组中,例如
var uk = [1,2,3,4,5,6,7,8],
us = [8,4,7,3,7,8,3,2],
fr = [4,6,8,3,2,6,8,4];
I want to create a master array, that contains all of these individual arrays, not concatenated/merged, so:
我想创建一个主数组,它包含所有这些单独的数组,而不是连接/合并,所以:
world = [uk, us, fr, etc]
How do I go about adding the arrays in such a manner so that they do not concatenate together? Note that there are hundreds of countries, and so I can't manually type them in, as above, and that I'm actually extracting them all from a single csv file, so can easily iterate over them as I extract them. Array.push seems to do the same as concat?
如何以这种方式添加数组,以便它们不会连接在一起?请注意,有数百个国家/地区,因此我无法手动输入它们,如上所述,并且我实际上是从单个csv文件中提取它们,因此可以在提取它们时轻松迭代它们。 Array.push似乎和concat一样吗?
Thanks
谢谢
3 个解决方案
#1
7
you can add multiple arrays to another array with push
您可以使用push将多个数组添加到另一个数组
var worlds = [];
worlds.push(uk);
worlds.push(us);
worlds.push(fr);
You would of course then reference the different subsets/arrays numerically i.e. worlds[0] = the 'uk' data
您当然会在数字上引用不同的子集/数组,即worlds [0] ='uk'数据
You could use an object
instead, that way you can access them with a string key
and make the code more readable. i.e.
您可以使用对象,这样您就可以使用字符串键访问它们,并使代码更具可读性。即
var worlds = {
"uk" : uk,
"us" : us
};
and access the data like:
并访问如下数据:
worlds.uk // will be the uk dataset/array
or
要么
worlds["uk"] // which allows you to store "uk" as a variable
N.B. Although not the question, I see you're using D3. D3 has a json method which reads in a json file and uses that as it's data. You might be better of using a json object to hold your data and passing that stright into D3. Here's the D3 docs for .json if it helps.
注:虽然不是问题,但我看到你正在使用D3。 D3有一个json方法,它读取json文件并将其用作数据。您最好使用json对象来保存数据并将其直接传递到D3中。这是.json的D3文档,如果有帮助的话。
It's also possible to pass a csv file to D3, which if you are not editing your data soruce might also be a solution
也可以将csv文件传递给D3,如果你不编辑你的数据,那么也可能是一个解决方案
#2
1
Array.push seems to do the same as concat?
Array.push似乎和concat一样吗?
Not really, this is why it's different methods. What you indeed want is push. Maybe like this:
不是真的,这就是为什么它的方法不同。你真正想要的是推动。也许是这样的:
world.push(uk, us, fr);
#3
0
You could very easily add the arrays to another array like so:
您可以非常轻松地将数组添加到另一个数组,如下所示:
var world = [
uk,
us,
fr,
etc
];
Which is essentially what you have already typed up. It will most definitely work and will not concatenate them together.
这基本上就是你已输入的内容。它肯定会起作用,不会将它们连接在一起。
#1
7
you can add multiple arrays to another array with push
您可以使用push将多个数组添加到另一个数组
var worlds = [];
worlds.push(uk);
worlds.push(us);
worlds.push(fr);
You would of course then reference the different subsets/arrays numerically i.e. worlds[0] = the 'uk' data
您当然会在数字上引用不同的子集/数组,即worlds [0] ='uk'数据
You could use an object
instead, that way you can access them with a string key
and make the code more readable. i.e.
您可以使用对象,这样您就可以使用字符串键访问它们,并使代码更具可读性。即
var worlds = {
"uk" : uk,
"us" : us
};
and access the data like:
并访问如下数据:
worlds.uk // will be the uk dataset/array
or
要么
worlds["uk"] // which allows you to store "uk" as a variable
N.B. Although not the question, I see you're using D3. D3 has a json method which reads in a json file and uses that as it's data. You might be better of using a json object to hold your data and passing that stright into D3. Here's the D3 docs for .json if it helps.
注:虽然不是问题,但我看到你正在使用D3。 D3有一个json方法,它读取json文件并将其用作数据。您最好使用json对象来保存数据并将其直接传递到D3中。这是.json的D3文档,如果有帮助的话。
It's also possible to pass a csv file to D3, which if you are not editing your data soruce might also be a solution
也可以将csv文件传递给D3,如果你不编辑你的数据,那么也可能是一个解决方案
#2
1
Array.push seems to do the same as concat?
Array.push似乎和concat一样吗?
Not really, this is why it's different methods. What you indeed want is push. Maybe like this:
不是真的,这就是为什么它的方法不同。你真正想要的是推动。也许是这样的:
world.push(uk, us, fr);
#3
0
You could very easily add the arrays to another array like so:
您可以非常轻松地将数组添加到另一个数组,如下所示:
var world = [
uk,
us,
fr,
etc
];
Which is essentially what you have already typed up. It will most definitely work and will not concatenate them together.
这基本上就是你已输入的内容。它肯定会起作用,不会将它们连接在一起。