# Thread: arr=[1,2,3,4,5,6] will be arr = [ [1,2], [3,4], [5,6] ];

1. ## arr=[1,2,3,4,5,6] will be arr = [ [1,2], [3,4], [5,6] ];

arr=[1,2,3,4,5,6] will be arr = [ [1,2], [3,4], [5,6] ];
Message box is empty. What can I do? Thanks...
Code:
```
<script type="text/javascript">

Array.grupla=function(arr,grup,numberOfEleman){
var A=[];
for(var i=0; i<arr.length; i++) {
for(var k=0; k<grup.length; k++){
for(var n=0; n<numberOfEleman.length; n++) {
A[k][n]=dizi[i];
}
}
}
return A;
}
var arr=[1,2,3,4,5,6];
arr=Array.grupla(arr,3,2);

</script>```

2. The k loop goers until k < grup.length
What is grup and does it have a length?

3. Code:
```Array.prototype.groupsof= function(n){
var A= this.slice(0), B=[];
while(A.length) {
B.push(Array(A.splice(0,n)));
}
return B;
}```
var A=[1,2,3,4,5,6];

4. grup.length = 3;
arr=Array.grupla(arr,3,2);

arr = [
[1,2],
[3,4],
[5,6]
];

5. mrhoo
I want message box displays
But it displays undefined. How can I do?

6. Originally Posted by mrhoo
Code:
```Array.prototype.groupsof= function(n){
var A= this.slice(0), B=[];
while(A.length) {
B.push(Array(A.splice(0,n)));
}
return B;
}```
var A=[1,2,3,4,5,6];
Wow !

Sorry but I don't understand var A= this.slice(0)
I think you can do

PHP Code:
``` Array.prototype.groupsof= function(n){     var B=[];     while(this.length) {         B.push([this.splice(0,n)]);     }     return B;     } var A=[1,2,3,4,5,6]; alert(A.groupsof(3).join('\n'));  ```
or not ?

Bye.

7. There are lots of problems with the original code. Numbers don't have a length, so grup.length and numberofElem.length will just want to be grup and numberOfElem respectively. Before A[k][n] can be created, there needs to first exist an A[k], and what on earh is dizi?

Go with the code from mrhoo instead.

8. This code is doing what I wanted.
Code:
```
<script type="text/javascript">

var arr=[1,2,3,4,5,6];
var A=[];
var s=0, m=0;
for(var i=0; i<arr.length; i+=2){
A[s]=[];
for(var n=0; n<2; n++) {
A[s][n]=arr[m++];
}
s++;
}

</script>```

9. Originally Posted by muazzez
This code is doing what I wanted.
Code:
```
<script type="text/javascript">

var arr=[1,2,3,4,5,6];
var A=[];
var s=0, m=0;
for(var i=0; i<arr.length; i+=2){
A[s]=[];
for(var n=0; n<2; n++) {
A[s][n]=arr[m++];
}
s++;
}

</script>```

PHP Code:
``` Array.prototype.groupsof= function(n){     var A= this.slice(0), B=[];     while(A.length) {         B.push(Array(A.splice(0,n)));     }     return B;     } var A=[1,2,3,4,5,6];  var tmp= A.groupsof(2); alert(tmp[0][0][0]); alert(tmp[0][0][1]);  ```

10. Thanks everybody.
What a beautiful day!

11. Sorry but I don't understand var A= this.slice(0)
I wrote it that way so that you would not change the original array, but return a new array with the array groups you specify.

You can discard the original with A=A.groupsof(2), but every occasionally you may need to refer to the original array.
whisher wants to alert A[1][1], so he must set A=A.groupsof(2);

But it is certainly not required!

12. I'm not sure what exactly are you trying to achieve: should [1..8] be converted to "[[1, 2], [3, 4], [5, 6], [7, 8]]" or "[[[1, 2]], [[3, 4]], [[5, 6]]]". Anyways, this can be probably done simpler:

Code:
```Array.prototype.group = function(n) {
for(var i = 0, t = []; i < this.length; i += n)
t.push(this.slice(i, i + n));
return t;
}```

13. stereofrog,
Your code converted to [[1, 2], [3, 4], [5, 6]];

var arr=[1,2,3,4,5,6];
var s = arr.group(2);
alert(s.toSource()); // [[1, 2], [3, 4], [5, 6]];

14. Well, this is what you wanted isn't it?

15. Yes. I use all of them.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•