Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #25965
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: JS Array problem |
| Date | 2014-10-21 19:02 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <1819918.Z1tkqcoRQN@PointedEars.de> (permalink) |
| References | <calc4vFrm5iU1@mid.individual.net> <m250og$r1l$1@dont-email.me> <can99nFbrnaU1@mid.individual.net> |
Mel Smith wrote:
> "danca" <cyberdanny@tiscalinet.it> wrote […]:
>> Il 20/10/2014 23:11, Mel Smith ha scritto:
>> Maybe something like this?
>>
>> var a,b,c,d ;
>> var abcd = ["a","b","c","d"] ;
>> for (var i= 0; i<abcd.length; i++) {
>> eval(abcd[i]+"="+i);
>> }
>
> […]
> Yes, That's what I was looking for.
Hardly, as it is harder to debug and more error-prone. It will not work in
strict mode (the scope chain of eval code is empty then), which you should
be using in order to avoid common programming errors.
Also,
for (var i = 0, len = abcd.length; i < len; ++i)
is more efficient when “abcd.length” does not change;
for (var i = abcd.length; i--;)
is even more efficient if the array is not sparse and order does not matter.
If the array-like object is sparse, the most efficient way, where order
matters and non-index own properties are not expected, is probably
var indexes = Object.keys(abcd);
for (var i = 0, len = indexes.length; i < len; ++i)
{
var index = indexes[i];
abcd[index] = …;
}
(Array indexes are names of own enumerable properties. If non-index own
properties are expected, an additional test of the property name is
required.)
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
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
JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-20 15:11 -0600
Re: JS Array problem Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-20 22:25 +0000
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:33 -0600
Re: JS Array problem danca <cyberdanny@tiscalinet.it> - 2014-10-21 09:08 +0200
Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:42 +0200
Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 09:47 +0200
Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:51 +0200
Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 09:53 +0200
Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:52 +0200
Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 10:36 +0200
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:36 -0600
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:35 -0600
Re: JS Array problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-21 19:02 +0200
Re: JS Array problem "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-10-21 13:12 +0200
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:38 -0600
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:29 -0600
My 'actual' code "Mel Smith" <syntel@cox.net> - 2014-10-21 11:20 -0600
Re: My 'actual' code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-10-21 19:52 +0100
Re: My 'actual' code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-21 21:23 +0200
Re: JS Array problem Violet Deng <dengviolet@gmail.com> - 2014-10-28 02:03 -0700
Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-28 10:08 +0100
csiph-web