Web Analytics Made Easy -
StatCounter Nifty way of splicing an array - anybody know???? - CodingForum

Announcement

Collapse
No announcement yet.

Nifty way of splicing an array - anybody know????

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Nifty way of splicing an array - anybody know????

    I have a need to take an array...lets say we have this..

    var numtosplice = 2; // this is setable...

    I want to group from array1 into array2 based on what 'numtosplice' is.

    array1 = new Array ('what','hello','mary','john','peter','steve','tiger','funny')


    and I want to take that array, and get this...

    array2 = new Array(
    ['what','hello']
    ['mary','john']
    ['peter','steve']
    ['tiger','funny']
    )

    I know I can cycle thru array1 and set a counter and then do an inner loop.. but i am hoping there is a nifty and cooler way..

    any ideas.. perhaps splice and pop????

  • #2
    Code:
    Array.prototype.resplit=function(n){
    	var A= this, A1= [],n=parseInt(n);
    	if(!n) n=1; 
    	while(A.length) A1.push(A.splice(0,n)) 
    	return A1;
    }

    Comment


    • #3
      thansk.. seems to work. Will play with it.. Thanks again.
      Last edited by BrightNail; Sep 27, 2006, 02:46 PM.

      Comment

      Working...
      X