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


Groups > comp.lang.javascript > #19340

Re: How to I remove many items from an array

From "Tony" <tony.johansson@inport.com>
Newsgroups comp.lang.javascript
Subject Re: How to I remove many items from an array
Date 2013-04-18 17:55 +0200
Organization A noiseless patient Spider
Message-ID <kkp4rh$81o$1@dont-email.me> (permalink)
References <kkoig1$3nq$1@dont-email.me> <kkoj2l$7dd$1@dont-email.me> <1493417.ljR73ylGCY@PointedEars.de> <kkp4g4$54m$1@dont-email.me>

Show all headers | View raw



"Tony"  wrote in message news:kkp4g4$54m$1@dont-email.me...



"Thomas 'PointedEars' Lahn"  wrote in message
news:1493417.ljR73ylGCY@PointedEars.de...

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.


Does it matter for performance
if I use this
var item = hinderArray[i];
                  a.push(item);
or this
                  a.push(hinderArray[i];);

function BtnConfirm_click()
        {
            var j = 0;
            var a = [];

            //Save index for checkbox that is not checked
            for (i = 0; i < hinderArray.length; i++)
            {
                if (document.getElementById('chk' + i).checked)
                {
                   var item = hinderArray[i];
                   a.push(item);
                }
           }
           hinderArray = a;

            //Save the hinderArray that was checked in cookie
           setCookie('hinder', JSON.stringify(hinderArray));

           window.location.href = "Schema.html";
        }

//Tony

I noticed that javascript does not accept writing
  a.push(hinderArray[i];); 

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