Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20287

Re: Javascript shift()

Path csiph.com!usenet.pasdenom.info!goblin3!goblin1!goblin.stu.neva.ru!news.astraweb.com!border5.a.newsrouter.astraweb.com!xlned.com!feeder7.xlned.com!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2660419.M6LF0BO9E7@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Fri, 14 Jun 2013 15:01:15 +0200
User-Agent KNode/4.10.2
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: Javascript shift()
Newsgroups comp.lang.javascript
References <aa28fb0d-9a33-4649-a561-88127227e14b@y5g2000vbg.googlegroups.com> <kpeuq8$r22$1@dont-email.me>
MIME-Version 1.0
Lines 101
NNTP-Posting-Date 14 Jun 2013 15:01:19 CEST
NNTP-Posting-Host 0a5e580f.newsspool3.arcor-online.net
X-Trace DXC=OMffhj56C]l<6cDJZfMd_cMcF=Q^Z^V3h4Fo<]lROoRa8kF<OcfhCOkQel<@S5]R;dDZm8W4\YJNlb@mF9jNikdeBoKGPB=12Ag7noSkeRn7jd
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:20287

Show key headers only | View raw


Denis McMahon wrote:

> 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.

ACK.

> 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?

One can never know.

> An array is either ordered or unordered - if it's unordered it doesn't
> matter what order the elements are in,

Your logic is flawed – again.

> 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.

As that is a syntax error, it does not return anything.  Functions are 
declared with

  function identifier (parameter1, parameter2/*, …*/)
  {
    // …
  }

and called with

  identifier(argument1, argument2/* , … */);

[The number of arguments does not need to match the number of formal 
parameters.  If an argument is passed for which there is no parameter, its 
value is available via a numeric zero-based property of the “arguments” 
object in the function context.  If no argument is passed for a formal 
parameter, it is initialized with the “undefined” value.  Also, the value of 
the “arguments.length” property yields the number of arguments that have 
been passed to a function.]

It is not always necessary to declare a function as there are function 
expressions:

  array.sort(function (a, b) {
   // …
  });

or

  var sortNumerically = function (a, b) {
    return a - b;
  };

  array.sort(sortNumerically);

A function declaration and the latter approach has the advantage that an 
unchanging function needs only be created once.  The advantage of a function 
expression over a function declaration primarily is that its evaluation 
result (a Function instance reference) can be assigned as value to any 
property, allowing for namespacing.

[It is strongly recommended to define and use this trivial sort function, as 
the default is implementation-dependent and may sort the elements as string 
values instead.]

See also: <http://PointedEars.de/es-matrix#features>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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