0

I'm trying to push an array (multiple times) into another array. Instead of an array of arrays, I'm getting all the values from the multiple push attempts as a single array. I've tried pushing an array implicitly (i.e. push([val1,val2]), explicitly creating a new Array, then pushing the new Array. Here's the key part of the code:

var coordinates=[];
...
for(i=0;i<6;i++)
{
    ...
        for(var j=start;j<circum[i].length;j++)
        {
            var segmentCoords=[];
            ...
                if(segmentFlag===false)
                {                        
                   segmentCoords.push([i+1,segmentLength]);
                    segmentFlag=true;                        
                }
                ...
                if(segmentFlag===true)
                {
                    var tempArray=new Array(i+1,segmentLength);
                    segmentCoords.push(tempArray);
                    segmentLength+=sectorLength;
                    coordinates.push(segmentCoords);
                    segmentFlag===false;                        
                }
           ...
}

From the many stackoverflow questions/answers I've looked at, I expect my coordinates array to look something like this: [[val1, val2],[val3,val4],[val5,val6]]. Instead it's [val1,val2,val3,val4,val5,val6]. That is what I would expect if I were using .concat() or .apply().

Can anyone explain why my code isn't generating an array of arrays?

I've got the full code here https://jsfiddle.net/Seytom/7xm9s4qr/ in case you want to see more of it.

1
  • var superSetArray=[]; superSetArray.push([1,2]); superSetArray.push([3,4]); superSetArray.push([5,6]); console.log(superSetArray); - This code works exactly like you expect. Commented Nov 15, 2016 at 12:20

2 Answers 2

2

You seem to be fooled by your console.log. Notice the difference between these two statements:

console.log( 'string ' + [[1,2],[3,4]] ); // string, '1,2,3,4'

console.log( 'string ', [[1,2],[3,4]] ); // string, [[1,2],[3,4]]

Because you are coercing the array into a string, this is the result. Its the same as:

console.log( new Array([1,2],[3,4]).join(',') ); // 1,2,3,4

It's simply what arrays do when you join them, regardless of whether they are nested. It is better to log the array separately so you can explore it in your console, so simple print your string and then add the array as the second argument. (The console takes an infinite amount of arguments and will print them all as one statement - safari even prints the first as a special type if its a string so its clearer to read).

In short: push behaves exactly as expected, and your code should simply work as intended, but the printing to the console seems to leave a bit to be desired :).

Sign up to request clarification or add additional context in comments.

4 Comments

I remember when I first did this, oh the facepalm... +1
Thank you, that was exactly it.
Wow! Can't believe you originally showed a document.write() example. Really bad, best practice to impart.
@ScottMarcus Ow waw you are petty. I learn from my mistakes, and I'm proud of it. (For anyone reading this in the future, he is unhappy that I pointed out it is better to close an input tag and use quotation marks around attributes because it's safer, certainly for unexperienced users. Then he went into my history and found something to comment on. Sjeesh.)
0

Use Array.concat:

var arrA = [0];
var arrB = [1, 2];
while (arrA.length < 10) {
  arrA = arrA.concat(arrB)
}
console.log(arrA)

1 Comment

This is exactly the behaviour the OP wants to avoid, or seems to be having (see my answer below).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.