Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20284 > unrolled thread
| Started by | JT <jonas.thornvall@gmail.com> |
|---|---|
| First post | 2013-06-14 00:15 -0700 |
| Last post | 2013-06-14 15:03 +0200 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.javascript
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
| From | JT <jonas.thornvall@gmail.com> |
|---|---|
| Date | 2013-06-14 00:15 -0700 |
| Subject | Javascript shift() |
| Message-ID | <aa28fb0d-9a33-4649-a561-88127227e14b@y5g2000vbg.googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-06-14 11:29 +0000 |
| Message-ID | <kpeuq8$r22$1@dont-email.me> |
| In reply to | #20284 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-06-14 15:01 +0200 |
| Message-ID | <2660419.M6LF0BO9E7@PointedEars.de> |
| In reply to | #20286 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2013-06-14 15:03 +0200 |
| Message-ID | <kpf4bg$cid$1@news.albasani.net> |
| In reply to | #20284 |
On 2013-06-14 09:15, 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.
You're probably looking for the splice() method. To insert an element at
index 2:
var a = [2, 3, 5, 6];
a.splice(2, 0, 4);
// a is now [2, 3, 4, 5, 6]
shift()/unshift() and push()/pop() always work on the ends of an array;
you could think of them as specialized cases of splice().
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
> element [x][y].swap();
>
> It is not hard to swap elements but it make more sense then the
> noneflexible shift() function.
Why would you need shift() for element swapping? Something like this
should be sufficient (untested):
Array.prototype.swap = function (a, b) {
var tmp = this[a];
this[a] = this[b];
this[b] = tmp;
};
var a = [2, 3, 5, 4, 6];
a.swap(2, 3);
// a is now [2, 3, 4, 5, 6];
(BTW: better use Object.defineProperty - where supported - instead of
directly augmenting a native prototype.)
- stefan
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web