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


Groups > comp.lang.javascript > #19331 > unrolled thread

How to I remove many items from an array

Started by"Tony" <tony.johansson@inport.com>
First post2013-04-18 12:41 +0200
Last post2013-04-18 18:51 +0200
Articles 6 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#19331 — How to I remove many items from an array

From"Tony" <tony.johansson@inport.com>
Date2013-04-18 12:41 +0200
SubjectHow to I remove many items from an array
Message-ID<kkoig1$3nq$1@dont-email.me>
Hello!

I have an array define like this
var hinderArray = new Array();
hinderArray[i] = { Pid: value.pid, Beskr: value.beskr, Enable: false  };


In this code snippet I set the field Enable=true if the checkbox is checked.
I want instead to remove the whole item if the checkbox is not checked.
So assume that the hinderArray contains 10 items and only the first checkbox 
is checked
then the hinderArray should only contain one item with the one that was 
checked.

I have tried to use  hinderArray.splice(i,1); but that is not working.
How should I accomplish this ?

function BtnConfirm_click()
{
         for (i = 0; i < hinderArray.length; i++)
         {
                if (document.getElementById('chk' + i).checked)
                {
                    hinderArray[i].Enable = true;
                }
                else
                {
                    // tried with hinderArray.splice(i,1);
                }
         }
}

//Tony 

[toc] | [next] | [standalone]


#19332

From"Tony" <tony.johansson@inport.com>
Date2013-04-18 12:51 +0200
Message-ID<kkoj2l$7dd$1@dont-email.me>
In reply to#19331
I found the solution

  var i = remArray.length;
            while (i--)
            {
                if (remArray[i] != undefined)
                {
                    hinderArray.splice(remArray[i], 1);
                }
            }


"Tony"  wrote in message news:kkoig1$3nq$1@dont-email.me...

Hello!

I have an array define like this
var hinderArray = new Array();
hinderArray[i] = { Pid: value.pid, Beskr: value.beskr, Enable: false  };


In this code snippet I set the field Enable=true if the checkbox is checked.
I want instead to remove the whole item if the checkbox is not checked.
So assume that the hinderArray contains 10 items and only the first checkbox
is checked
then the hinderArray should only contain one item with the one that was
checked.

I have tried to use  hinderArray.splice(i,1); but that is not working.
How should I accomplish this ?

function BtnConfirm_click()
{
         for (i = 0; i < hinderArray.length; i++)
         {
                if (document.getElementById('chk' + i).checked)
                {
                    hinderArray[i].Enable = true;
                }
                else
                {
                    // tried with hinderArray.splice(i,1);
                }
         }
}

//Tony 

[toc] | [prev] | [next] | [standalone]


#19335

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-18 15:09 +0200
Message-ID<1493417.ljR73ylGCY@PointedEars.de>
In reply to#19332
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.

[toc] | [prev] | [next] | [standalone]


#19338

From"Tony" <tony.johansson@inport.com>
Date2013-04-18 17:49 +0200
Message-ID<kkp4g4$54m$1@dont-email.me>
In reply to#19335

"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 

[toc] | [prev] | [next] | [standalone]


#19340

From"Tony" <tony.johansson@inport.com>
Date2013-04-18 17:55 +0200
Message-ID<kkp4rh$81o$1@dont-email.me>
In reply to#19338

"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];); 

[toc] | [prev] | [next] | [standalone]


#19356

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-18 18:51 +0200
Message-ID<3638854.9QfiOD8MEG@PointedEars.de>
In reply to#19338
Tony wrote:

> [perhaps something new]

<http://jibbering.com/faq/#posting>
<http://PointedEars.de/faq#posting>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web