Is there no better way to merge one array into another than iterating over the array you wish to append with each() and push()ing the elements on to the other array? Here''s what I mean: var arr1 = [1, 2, 3, 4, 5]; var arr2 = [''a'', ''b'', ''c'']; $A(arr2).each(function(el) { arr1.push(el) }); Is there no better way to do it than this? Thanks! -Jeremy _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Maybe an alternative: var res = $A([arr1,arr2]).flatten(); hth sigi On 4/24/06, Jeremy Kitchen <kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org> wrote:> > Is there no better way to merge one array into another than iterating over > the > array you wish to append with each() and push()ing the elements on to the > other array? > > Here''s what I mean: > var arr1 = [1, 2, 3, 4, 5]; > var arr2 = [''a'', ''b'', ''c'']; > $A(arr2).each(function(el) { arr1.push(el) }); > > Is there no better way to do it than this? > > Thanks! > > -Jeremy > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > >-- Mit freundlichen Grüßen Siegfried Puchbauer _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On Monday 24 April 2006 12:16, Siegfried Puchbauer wrote:> Maybe an alternative: var res = $A([arr1,arr2]).flatten();I thought about that, and while that might be easier to write, I''m looking more for efficiency. I''ll just stick with the each()/push() iterator for now :) -Jeremy _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On 4/24/06, Jeremy Kitchen <kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org> wrote:> Is there no better way to merge one array into another than iterating over the > array you wish to append with each() and push()ing the elements on to the > other array?Anything wrong with the standard Javascript arr1.concat(arr2) ? :-) Bye, Martin
On 4/24/06, Martin Bialasinski <klingeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 4/24/06, Jeremy Kitchen <kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org> wrote: > > Is there no better way to merge one array into another than iterating over the > > array you wish to append with each() and push()ing the elements on to the > > other array? > > Anything wrong with the standard Javascript arr1.concat(arr2) ? :-)Just to add to this ... it should be pointed out that this returns a new array (counter intuitive, if you ask me). arr1 = arr1.concat(arr2); If you need to append the contents of arr2 to arr1 without modifying your reference to arr1, then you need to loop through arr2 and push onto arr1. Todd
On Monday 24 April 2006 14:36, Todd Ross wrote:> On 4/24/06, Martin Bialasinski <klingeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 4/24/06, Jeremy Kitchen <kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org> wrote: > > > Is there no better way to merge one array into another than iterating > > > over the array you wish to append with each() and push()ing the > > > elements on to the other array? > > > > Anything wrong with the standard Javascript arr1.concat(arr2) ? :-)aha! just what I was looking for!> Just to add to this ... it should be pointed out that this returns a > new array (counter intuitive, if you ask me). > > arr1 = arr1.concat(arr2); > > If you need to append the contents of arr2 to arr1 without modifying > your reference to arr1, then you need to loop through arr2 and push > onto arr1.fortunately, for my purposes concat will do fine :) -Jeremy -- Jeremy Kitchen ++ kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org http://ipaction.org/ -- defend your rights to fair use _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs