Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #30286
| Path | csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Aleksandro <aleksandro@gmx.com> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: duplicator an array |
| Date | Sat, 16 Apr 2016 14:13:31 -0300 |
| Organization | A noiseless patient Spider |
| Lines | 80 |
| Message-ID | <netrli$9dr$1@dont-email.me> (permalink) |
| References | <ea578169-9d58-4249-91b4-e0cbad5a6fd8@googlegroups.com> <nen4e4$ggn$1@dont-email.me> <netb2m$42n$1@news.albasani.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Sat, 16 Apr 2016 17:10:10 -0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="7363fa7664fe264696fafdff2cecae6a"; logging-data="9659"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xnQGBMmbiA5OA9XHJDN6nEX1ZhftvLJM=" |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.1 |
| In-Reply-To | <netb2m$42n$1@news.albasani.net> |
| Cancel-Lock | sha1:ZEfCj873aiTfbSkkz9prQPMz7h0= |
| Xref | csiph.com comp.lang.javascript:30286 |
Show key headers only | View raw
On 16/04/16 09:27, Stefan Weiss wrote:
> Aleksandro wrote:
>> On 13/04/16 22:27, JRough wrote:
>>> var arr =[1,2,3,4,5];
>>> function duplicator(arr){
>>> return arr.concat(arr.slice(0));
>>> }
>>> var res = duplicator(arr);
>
> ...
>
>> Try:
>>
>> Array.prototype.duplicate = function ()
>> {
>> this.push.apply(this, this.slice(0))
>> }
>
> Note that this is not equivalent to OP's function.
>
> 1) push() modifies the original array, concat() does not.
>
> 2) It handles sparse arrays badly:
>
> "use strict";
>
> let a = [0,,,,4],
> b = a.slice(0);
>
> dir(a, "a");
> dir(b, "b");
> dir(a.concat(b), "concat()");
> a.push.apply(a, b);
> dir(a, "push() applied");
>
> function dir (arr, label)
> {
> console.log(label + ":");
> for (let i in arr) console.log(` [${i}] = ${arr[i]}`);
> }
>
> Output:
>
> a:
> [0] = 0
> [4] = 4
> b:
> [0] = 0
> [4] = 4
> concat():
> [0] = 0
> [4] = 4
> [5] = 0
> [9] = 4
> push() applied:
> [0] = 0
> [4] = 4
> [5] = 0
> [6] = undefined
> [7] = undefined
> [8] = undefined
> [9] = 4
>
> This is because apply() fills in `undefined` for missing parameters.
>
>
> By the way, ES2015 introduced the spread operator `...`.
> With this operator, you can also write
>
> this.push(...this.slice(0))
>
> instead of
>
> this.push.apply(this, this.slice(0))
>
>
> -- stefan
It could have been what the OP intended though.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
duplicator an array JRough <janis.rough@gmail.com> - 2016-04-13 18:27 -0700
Re: duplicator an array Aleksandro <aleksandro@gmx.com> - 2016-04-14 01:00 -0300
Re: duplicator an array JRough <janis.rough@gmail.com> - 2016-04-14 09:53 -0700
Re: duplicator an array "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-04-14 19:41 +0200
Re: duplicator an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-14 22:09 +0200
Re: duplicator an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-14 22:23 +0200
Re: duplicator an array Stefan Weiss <krewecherl@gmail.com> - 2016-04-16 14:27 +0200
Re: duplicator an array Joao Rodrigues <jr@none.com> - 2016-04-16 12:01 -0300
Re: duplicator an array Joao Rodrigues <jr@none.com> - 2016-04-16 13:43 -0300
Re: duplicator an array Aleksandro <aleksandro@gmx.com> - 2016-04-16 14:13 -0300
Re: duplicator an array bharat.bandgar009@gmail.com - 2017-05-03 01:33 -0700
csiph-web