SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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);
    alert(arr);
    
    </script>
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The k loop goers until k < grup.length
    What is grup and does it have a length?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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];
    alert(A.groupsof(2).join('\n'));

  4. #4
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    grup.length = 3;
    arr=Array.grupla(arr,3,2);

    arr = [
    [1,2],
    [3,4],
    [5,6]
    ];
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mrhoo
    your code works.
    I want message box displays
    alert(A[1][1]); // returns 4
    But it displays undefined. How can I do?
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  6. #6
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    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];
    alert(A.groupsof(2).join('\n'));
    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. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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++;
    }
    
    alert(A);
    alert(A[1][1]);  // 4
    
    </script>
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  9. #9
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by muazzez View Post
    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++;
    }
    
    alert(A);
    alert(A[1][1]);  // 4
    
    </script>

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

  10. #10
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everybody.
    What a beautiful day!
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  11. #11
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!
    Last edited by mrhoo; Apr 8, 2008 at 09:07.

  12. #12
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok

  13. #13
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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;
    }

  14. #14
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stereofrog,
    Your code converted to [[1, 2], [3, 4], [5, 6]];


    var arr=[1,2,3,4,5,6];
    alert(arr.toSource()); // [1,2,3,4,5,6]
    var s = arr.group(2);
    alert(s.toSource()); // [[1, 2], [3, 4], [5, 6]];
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  15. #15
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, this is what you wanted isn't it?

  16. #16
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. I use all of them.
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.


Bookmarks

Posting Permissions

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