1

I don't want to do something like below, or is that my only option?

var tempArray = [];
var tempArray2 = [];

for(var j = 0; j < data.length; j++) {
    tempArray2 = [];
    for(var k = 0; k < data[0].length; k++) {
        tempArray2.push(data[j][k]);
    }
    tempArray.push( tempArray2 );
}
nortond
  • 23
  • 5
  • I don't see the `N` in your code. Also, I'm assuming you meant `data[j].length` in your inner `for-loop`. – Daniel May 22 '14 at 20:47
  • While there may be something more concise, this looping fundamentally happens. – Jason McCreary May 22 '14 at 20:47
  • This doesn't make much sense. Your `tempArray` is going to hold `j` references to `tempArray2`. They'll all be referencing exactly the same Array with all the same members.. – cookie monster May 22 '14 at 20:55
  • That is true too. He is not re-setting the reference of `tempArray2` to a new Array object. – Andrei May 22 '14 at 20:56
  • You are right about not re-setting the reference. Just fixed that. Thanks! – nortond May 22 '14 at 21:00
  • In that case, if all you wanted to do was to have a shallow new Array that contains a copy of each `data` Array, then do this: `tempArray = data.map(function(arr) { return arr.slice(); });` – cookie monster May 22 '14 at 21:06

1 Answers1

1

You could use the apply method and push the entire array at once. Never tried with a matrix like array.

Example:

var tempArray = [], tempArray2 = [];

for(var j = 0, dataLen = data.length; j < dataLen; j++) {
    tempArray2.push.apply(tempArray2, data[j]);
    tempArray.push(tempArray2), tempArray2 = [];
}

jsFiddle:

http://jsfiddle.net/k264S/6/

Or as pointed by cookie monster using slice() without a parameter will create a shallow copy (it will not copy the values at references too).

Example:

var tempArray = [];

for(var j = 0, dataLen = data.length; j < dataLen; j++) {
    tempArray.push(data[j].slice());
}

This will work as long as your data[j] has less than ~150.000 elements in it.

If it has more, you should use the technique mentioned in this answer: How to extend an existing JavaScript array with another array, without creating a new array?

Community
  • 1
  • 1
Andrei
  • 3,086
  • 2
  • 19
  • 24
  • `tempArray.push.apply(tempArray, [data[j]]);` is the same as `tempArray.push(data[j]);`. Did you intend to put `data[j]` in `[]`? – cookie monster May 22 '14 at 20:57
  • It is not the same. Without the `[]` `tempArray` will be a one-dimensional array with 10000 elements. With `[]` it will be a matrix exactly like data. – Andrei May 22 '14 at 20:58
  • Read my comment again. You're right if you did `tempArray.push.apply(tempArray, data[j]);`, then it would be a single flat Array, but that's not what I said it was the same as. Your version of `.apply()` is the same as doing `tempArray.push(data[j]);` – cookie monster May 22 '14 at 21:00
  • Oh, sorry, misread. But it is not the same. if you do `tempArray.push(data[j]);` the reference will be the same. So no advantage. If you do `tempArray.push.apply(tempArray, [data[j]]);` the reference will be different. It will be a new matrix all together. – Andrei May 22 '14 at 21:01
  • Nope, same reference either way. The Array is never copied. All you're doing with `.apply` is saying *"take this collection of arguments and pretend as though I passed them individually"*. In other words, `foo.push.apply(foo, [bar, baz, buz])` is exactly the same as `foo.push(bar, baz buz);` – cookie monster May 22 '14 at 21:04
  • That is true, but since I am wrapping it in a new `array` each element `data[j][k]` (if it is a primitive) will be added to it, so the reference would be new. http://jsfiddle.net/k264S/3/ if you look at this output you will see that this is not the same reference. – Andrei May 22 '14 at 21:10
  • No, you're not adding each `data[j][k]` to the `[]` wrapper. That is discarded. The `.apply()` method merely takes the members of the collection and use them as though they were individual arguments. There's no other use for the outer `[]`. – cookie monster May 22 '14 at 21:12
  • ...your demo shows that the outer Array that holds each `data[j]` is different. That's because you're pushing each `data[j]` into a different Array. But each `tempArray[j]` is going to be a same reference to each corresponding `data[j]`. No copy is made. – cookie monster May 22 '14 at 21:14
  • ...[here's a demo](http://jsfiddle.net/k264S/4/) that compares each member of `tempArray` to each member of `data`. They're all the same. Point is that there's no difference between `foo.push(x)` and `foo.push.apply(foo, [x]);` They're exactly identical – cookie monster May 22 '14 at 21:15
  • Yes, checked that after my last comment and was looking for another way around that. – Andrei May 22 '14 at 21:18
  • If your intent is to make a copy of each `data[j]`, then just do `tempArray.push(data[j].slice());`, which will make a shallow copy of each `data[j]` Array. – cookie monster May 22 '14 at 21:20
  • For some reason I thought of the `[]` literal as a copy constructor that would do the copy. So using `slice()` to force the copy is the more elegant way apparently. Added it to the answer with your credit. Thank you. – Andrei May 22 '14 at 21:28