Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #19335
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!news.internetdienste.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| Content-Type | text/plain; charset="UTF-8" |
| Message-ID | <1493417.ljR73ylGCY@PointedEars.de> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <usenet@PointedEars.de> |
| Organization | PointedEars Software (PES) |
| Date | Thu, 18 Apr 2013 15:09:56 +0200 |
| User-Agent | KNode/4.8.5 |
| 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: How to I remove many items from an array |
| Newsgroups | comp.lang.javascript |
| References | <kkoig1$3nq$1@dont-email.me> <kkoj2l$7dd$1@dont-email.me> |
| MIME-Version | 1.0 |
| Lines | 90 |
| NNTP-Posting-Date | 18 Apr 2013 15:09:59 CEST |
| NNTP-Posting-Host | bd27c3fa.newsspool1.arcor-online.net |
| X-Trace | DXC=>3fbHZ@0dDj:i=48;n?Z:`ic==]BZ:afn4Fo<]lROoRankgeX?EC@@`E^]7Dg`Z:QjDZm8W4\YJNlb@mF9jNikdeIQbgJMhLo0gmh>fP9m7jNk |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | csiph.com comp.lang.javascript:19335 |
Show key headers only | View raw
Tony wrote:
> I found the solution
You have found *a* solution; not the best one. Not even a good one.
> var i = remArray.length;
> while (i--)
> {
> if (remArray[i] != undefined)
Although “undefined” can be considered a safe *feature* now, you may want to
use
if (typeof remArray[i] != "undefined")
for maximum backwards-compatibility and value-safety (in previous
implementations, “undefined” could be overwritten if it was supported).
> {
> hinderArray.splice(remArray[i], 1);
^^^^^^^^^^^
Avoid inefficient repeated MemberExpressions for the same value; use
variables.
> }
> }
This removes all elements that are not already undefined or null from the
array, not just those where the “Enable” property (should be “enable”, BTW)
is “false”.
Anyhow, if there *many* items to be removed (this was your premise), or if
the array is large, this approach is inefficient, because the array is
reorganized in each step, that is –
\ index | 0 | 1 | 2 | 3
step \ | | | |
------------+-----------+-------+-------+-----------
0 | "foo" | "bar" | "baz" | "bla"
1 | [deleted] | "bar" | "baz" | "bla"
2 | "bar" | "bar" | "baz" | "bla"
3 | "bar" | "baz" | "baz" | "bla"
4 | "bar" | "baz" | "bla" | "bla"
5 | "bar" | "baz" | "bla" | [deleted]
– if only "foo" is to be removed. *Five* steps for removing *one* element
if three elements follow it.
It is much more efficient then (both memory-wise and runtime-wise) to create
a new Array instance and fill it only with the values that match:
var a = [];
for (var i = 0, len = hinderArray.length; i < len; ++i)
{
var item = hinderArray[i];
/* item could be null */
if (item && item.Enable)
{
a.push(item);
}
}
hinderArray = a;
or, with ECMAScript Edition 5 and later:
hinderArray = hinderArray.filter(function (item) {
return item && item.Enable;
});
The “filter” method is more efficient with sparse arrays (because it only
operates on defined elements), but probably less efficient (due to repeated
callback calls) with other ones. JSX:array.js includes a (not yet unit-
tested) implementation, jsx.array.filter(), that by default augments
Array.prototype:
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Farray.js>
> [Top post]
Please don't.
--
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
How to I remove many items from an array "Tony" <tony.johansson@inport.com> - 2013-04-18 12:41 +0200
Re: How to I remove many items from an array "Tony" <tony.johansson@inport.com> - 2013-04-18 12:51 +0200
Re: How to I remove many items from an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-18 15:09 +0200
Re: How to I remove many items from an array "Tony" <tony.johansson@inport.com> - 2013-04-18 17:49 +0200
Re: How to I remove many items from an array "Tony" <tony.johansson@inport.com> - 2013-04-18 17:55 +0200
Re: How to I remove many items from an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-18 18:51 +0200
csiph-web