Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20286
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Javascript shift() |
| Date | 2013-06-14 11:29 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <kpeuq8$r22$1@dont-email.me> (permalink) |
| References | <aa28fb0d-9a33-4649-a561-88127227e14b@y5g2000vbg.googlegroups.com> |
On Fri, 14 Jun 2013 00:15:37 -0700, JT wrote:
> Can i somehow tell which javascript elements to shift?
> Is something like following possible element[i].shift()
>
> Many times one want to change items not necessarily at element[0], and
> sometimes you want to swap them with another index.
>
> I think the shift function should take one or two parameters or a new
> function should be constructed.
>
> element [x][y].swap();
>
> It is not hard to swap elements but it make more sense then the
> noneflexible shift() function.
If you want to swap elements x and y in an array:
element [x][y].swap();
makes no sense.
Your function would have to be something like:
array.swap_elements(x,y);
But why would you ever need to do this except in the context of sorting?
An array is either ordered or unordered - if it's unordered it doesn't
matter what order the elements are in, and if you want it ordered
according to some specific algorithm, just write a custom sort function
and sort with it:
array.sort(sortfunction);
where:
sortfunction( a, b ) {}
returns <0 if a comes first, >0 if b comes first, 0 if they're equal.
--
Denis McMahon, denismfmcmahon@gmail.com
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Javascript shift() JT <jonas.thornvall@gmail.com> - 2013-06-14 00:15 -0700
Re: Javascript shift() Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-14 11:29 +0000
Re: Javascript shift() Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-14 15:01 +0200
Re: Javascript shift() Stefan Weiss <krewecherl@gmail.com> - 2013-06-14 15:03 +0200
csiph-web