Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19335

Re: How to I remove many items from an array

Message-ID <1493417.ljR73ylGCY@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-04-18 15:09 +0200
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>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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