Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: JS Array problem Date: Tue, 21 Oct 2014 19:02:34 +0200 Organization: PointedEars Software (PES) Lines: 48 Message-ID: <1819918.Z1tkqcoRQN@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1413910968 28921 eJwFwYEBgDAIA7CXwEKVcxis/59gkqBz3mAyUikvUey5D6aC3rIYXBqr79lNP7NlBKD9hB8oIRGT (21 Oct 2014 17:02:48 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 21 Oct 2014 17:02:48 +0000 (UTC) User-Agent: KNode/4.14.1 X-User-ID: eJwFwQcBwDAMAzBKy7HTwmkefwiTYBRWOEHHYvtkV0LjmhRSzpi1jOsn0cOZRttE1DuXncN9+i195cXVxA9qjxYp Cancel-Lock: sha1:lzkVCwovuueOTLzeBLmNqfl1Zl8= X-NNTP-Posting-Host: eJwNyskBwCAIBMCW1sCilEM4+i9Bf/MYii3LrUZTDufoh5HS47AkTrJjOSo0GuL9k29EiW7i+QIKQBA1 Xref: csiph.com comp.lang.javascript:25965 Mel Smith wrote: > "danca" 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> 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: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not Cc: me. / Bitte keine Kopien per E-Mail.