-3

I've to do a strange thing and I don't know if is possible.

Let assume I've one aray

MasterArray = [1,2,3,4];

Now for each MasterArray item I need to have multiple insertion, for example under the item 1 I've to push N value, for example the MasterArray[0] must have this correlations 5,8,3,9 ...

This for any items on MasterArray.

My first idea is to create a new array one for each MasterArray items, something like this

var newobject = X;

for (i = 0; i < MasterArray.length; i++) {
Arr[i] = push the newobject ;
}

But I don't think that is a good way!

The purpose it to have a kind of correlated array.

MasterArray[0] is correlated to another array [5,8,3,9, ...]
MasterArray[1] is correlated to another array [5,6,7,1, ...]
MasterArray[2] is correlated to another array [7,45,23,2, ...]

And so on

I hope to have explained myself

Jorman Franzini
  • 329
  • 1
  • 3
  • 17
  • 3
    So you want to make a 2D array? – Carcigenicate Sep 24 '16 at 12:06
  • 2
    please specify some input and the wanted output. you may have a look here: [mcve] – Nina Scholz Sep 24 '16 at 12:08
  • I don't know if is a 2d array but the 1st array element have more that one element correlated on it – Jorman Franzini Sep 24 '16 at 12:09
  • 1
    how do they correlate to each other? – Nina Scholz Sep 24 '16 at 12:13
  • @JormanFranzini to have more than one element at ain index in an array, you need to have another array there (or some other data structure, but arrays are most common), e.g., `[ [1, 2], [3, 4] ]` will be an array that has two elements at each index. If an array only contains other arrays, it's called a 2D array. You can have N-dimensional arrays depending on how deep you nest the arrays but 2D are the most frequently found – VLAZ Sep 24 '16 at 12:15
  • 1
    Actually now when I re-read your question, I'm thinking you may just need an object. If you have an array of values, e.g., `[ 1, 2, 3]` and you want each of those values to have a relationship with an array, then that's pretty much what objects are for `{ 1: [1, 11, 111], 2: [2, 22, 222], 3: [3, 33, 333]}` - they do express the relationship between their key and the values associated with the key. – VLAZ Sep 24 '16 at 12:22
  • @vlaz I think that is the way, the 1st element of the masterarray, contain more value. So how can I push and manipulate this object? – Jorman Franzini Sep 24 '16 at 12:29

1 Answers1

1

Just create a 2D array in this way:

var myArray = new Array(5); // For example 5;
for (var i = 0; i < myArray.length; i++) {
  myArray[i] = new Array(10);
}

Or, if you don't need to specify any size:

var myArray = new Array(5); // For example 5;
for (var i = 0; i < myArray.length; i++) {
  myArray[i] = [];
}

EDIT:

For manipulate you just need to use innested loops:

for (var i = 0; i < myArray.length; i++) {
   for (var j = 0; i < myArray[i].length; j++) {  
        myArray[i][j] = x; // where x is some variable
}

For add elements in the back just use .push() method:

myArray[0].push(5);
granmirupa
  • 2,780
  • 16
  • 27
  • Using ES6 it's even simpler: `Array(5).fill([])` – VLAZ Sep 24 '16 at 12:12
  • I know, hence why I specified it's ES6 - ES6 itself is not supported everywhere. However, since it will be at some point in the (not so distant) future, I thought it's worth mentioning it. – VLAZ Sep 24 '16 at 12:18
  • ES6 `Array(5).fill([])` will not work since `.fill([])` will fill the master array with the same arrays which will reference each other. – Redu Sep 24 '16 at 12:26
  • @Redu damn, you're right. I thought that it puts a new one in each index. – VLAZ Sep 24 '16 at 12:31
  • 1
    @vlaz instantiating an ND array in JS is not as simple as it seems. Check [this](http://stackoverflow.com/a/38940684/4543207) – Redu Sep 24 '16 at 12:39
  • I don't know if is correct for my case. maybe the @vlaz solution is better. I don't know then how push and manipulate the others array, because after the creation I always need to manipulate them – Jorman Franzini Sep 24 '16 at 12:41
  • I mean this { 1: [1, 11, 111], 2: [2, 22, 222], 3: [3, 33, 333]} This seems to be the solution. The 1st array have correlated X values, the 2nd the same. I only need a way to create it and manipulate it – Jorman Franzini Sep 24 '16 at 12:50
  • @JormanFranzini is exactly what my solution does. Did you try to use? – granmirupa Sep 24 '16 at 12:54
  • Not yet, I'll try. In my case I don't know how long is the 1st array. One question can I check the length of the inner array? Something like this I suppose myArray[0].length That works? – Jorman Franzini Sep 24 '16 at 13:02
  • @JormanFranzini Yeah it works, as u can see I used in the example – granmirupa Sep 24 '16 at 13:04