1

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.

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?

Thanks

2
  • 1
    Wouldn't an object be a better solution? That way if there's hundreds of entries you could just use world['uk'] rather than iterating over a list?
    – NMunro
    Commented Jul 22, 2015 at 15:20
  • Is there any structure that holds all the country abbreviations or are they all just defined like var uk = [...]?
    – Dan
    Commented Jul 22, 2015 at 15:20

3 Answers 3

9

you can add multiple arrays to another array with 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

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.

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

3
  • using push instead of just adding the inner arrays as indexes on the declaration of world is more memory intensive (hardly though). Also, iterating over an object is entirely different than what @NorthLaine is asking for and more intensive than using an array. Although it is what I would do, I do not think it is a valid answer Commented Jul 22, 2015 at 15:25
  • Not sure what you mean about iteratingover an object? I havent mentioned it, and the OP only mentions that he/she is iterating over the cvs, not the data
    – atmd
    Commented Jul 22, 2015 at 15:30
  • Push was indeed the right thing to do. My problem was I was assigning my created arrays the same name in the statement that looped over the csv file, hence they were being appended to the same array as oppose to behaving as desired. Now the variable names are dynamically generated. Thanks! Commented Jul 28, 2015 at 11:28
1

Array.push seems to do the same as concat?

Not really, this is why it's different methods. What you indeed want is push. Maybe like this:

world.push(uk, us, fr);
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.