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


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

Empty array

Started byArchos <raul.san@sent.com>
First post2012-02-25 10:21 -0800
Last post2012-03-01 21:20 +0000
Articles 11 — 7 participants

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


Contents

  Empty array Archos <raul.san@sent.com> - 2012-02-25 10:21 -0800
    Re: Empty array Gregor Kofler <usenet@gregorkofler.com> - 2012-02-25 23:02 +0100
    Re: Empty array Dr J R Stockton <reply1208@merlyn.demon.co.uk> - 2012-02-26 20:50 +0000
      Re: Empty array "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-27 14:46 +0000
        Re: Empty array Dr J R Stockton <reply1209@merlyn.demon.co.uk> - 2012-02-28 23:42 +0000
    Re: Empty array Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-02-27 07:30 -0800
      Re: Empty array Dr J R Stockton <reply1209@merlyn.demon.co.uk> - 2012-02-28 22:00 +0000
        Re: Empty array Gene Wirchenko <genew@ocis.net> - 2012-02-28 17:20 -0800
          Re: Empty array Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-02-29 01:23 -0800
            Re: Empty array Gene Wirchenko <genew@ocis.net> - 2012-02-29 14:12 -0800
          Re: Empty array Dr J R Stockton <reply1209@merlyn.demon.co.uk> - 2012-03-01 21:20 +0000

#12184 — Empty array

FromArchos <raul.san@sent.com>
Date2012-02-25 10:21 -0800
SubjectEmpty array
Message-ID<0f481e7c-97b9-4c76-9660-321681876658@w9g2000vbv.googlegroups.com>
Is there any way to know if an array is empty? Without using its
length method?

I thought that could work the nex one:

var a = []; if (a === []) {console.log("empty")}

[toc] | [next] | [standalone]


#12185

FromGregor Kofler <usenet@gregorkofler.com>
Date2012-02-25 23:02 +0100
Message-ID<jiblou$qum$1@dont-email.me>
In reply to#12184
Am 2012-02-25 19:21, Archos meinte:
> Is there any way to know if an array is empty? Without using its
> length method?

There is no length "method". Only a length property.
What do you mean by "empty"? That it has length zero? Then check the
length property.

> I thought that could work the nex one:
> 
> var a = []; if (a === []) {console.log("empty")}

You can't compare arrays (or any object within JS) this way. In this
case you compare an array reference stored in a with an ad-hoc created
array - which is a different object - and the comparison will always
return false. (If two variables reference the same object, their
equality comparison will return true).

Gregor


-- 
http://vxweb.net

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


#12194

FromDr J R Stockton <reply1208@merlyn.demon.co.uk>
Date2012-02-26 20:50 +0000
Message-ID<B4qvXYMZspSPFwE3@invalid.uk.co.demon.merlyn.invalid>
In reply to#12184
In comp.lang.javascript message <0f481e7c-97b9-4c76-9660-321681876658@w9
g2000vbv.googlegroups.com>, Sat, 25 Feb 2012 10:21:19, Archos
<raul.san@sent.com> posted:

>Is there any way to know if an array is empty? Without using its
>length method?

Since an array can have elements not indexed by 0 // (length-1),
A.length does not necessarily suffice.

>I thought that could work the nex one:
>
>var a = []; if (a === []) {console.log("empty")}

If you are uninterested in non-enumerable elements, how about something
based on

        S = false
        for (J in A) S = true           //      ?

-- 
(c) John Stockton, nr London UK.  ?@merlyn.demon.co.uk  Turnpike v4.00  MIME
  Prof Timo Salmi's Usenet Q&A <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqn.zip>
  TS FAQs via : http://www.uwasa.fi/~ts/http/ : tsfaq.html  quote margin &c.
  My page <URL:http://www.merlyn.demon.co.uk/news-use.htm> on usage of News.

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


#12199

From"Evertjan." <exjxw.hannivoort@interxnl.net>
Date2012-02-27 14:46 +0000
Message-ID<XnsA006A0638F746eejj99@194.109.133.133>
In reply to#12194
Dr J R Stockton wrote on 26 feb 2012 in comp.lang.javascript:

> In comp.lang.javascript message
> <0f481e7c-97b9-4c76-9660-321681876658@w9 g2000vbv.googlegroups.com>,
> Sat, 25 Feb 2012 10:21:19, Archos <raul.san@sent.com> posted:
> 
>>Is there any way to know if an array is empty? Without using its
>>length method?
> 
> Since an array can have elements not indexed by 0 // (length-1),
> A.length does not necessarily suffice.
> 
>>I thought that could work the nex one:
>>
>>var a = []; if (a === []) {console.log("empty")}
> 
> If you are uninterested in non-enumerable elements, how about
> something based on
> 
>         S = false
>         for (J in A) S = true           //      ?

use 'break' to unloop the loop for speed,
works for arrays and other objects:

function notEmpty(obj) {
   var x,result = false;
   for (x in obj) {
      result = true;
      break;
   };
   return result;
};

or multiple returns for the same result:

function notEmpty(obj) {
   var x;
   for (x in obj) 
      return true;
   return false;
};




-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#12261

FromDr J R Stockton <reply1209@merlyn.demon.co.uk>
Date2012-02-28 23:42 +0000
Message-ID<u1W0UzFuZWTPFwYD@invalid.uk.co.demon.merlyn.invalid>
In reply to#12199
In comp.lang.javascript message <XnsA006A0638F746eejj99@194.109.133.133>
, Mon, 27 Feb 2012 14:46:03, Evertjan. <exjxw.hannivoort@interxnl.net>
posted:

>use 'break' to unloop the loop for speed,
>works for arrays and other objects:
>

That is why I wrote "based on", leaving the details as an exercise for
the OP, quicumque sit.

-- 
 (c) John Stockton, nr London UK.  ?@merlyn.demon.co.uk  DOS 3.3 6.20 ; WinXP.
   Web  <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
   PAS EXE TXT ZIP via  <http://www.merlyn.demon.co.uk/programs/00index.htm>
   My DOS  <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

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


#12201

FromAsen Bozhilov <asen.bozhilov@gmail.com>
Date2012-02-27 07:30 -0800
Message-ID<e8bddc62-6b7f-4463-b0bb-51912eeca4ef@z31g2000vbt.googlegroups.com>
In reply to#12184
Archos wrote:
> Is there any way to know if an array is empty? Without using its
> length method?

The array is applicable when you need sequenced collection. In
sequenced collection the `lenght' property is sufficed to check for
emptiness.

There are no associative arrays in ECMAScript. The array instances are
regular native objects with a little bit different setter. In general
if you have to use sparse array or associative, better use object
instance. Then you can check it with `for-in`, whether is empty.




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


#12260

FromDr J R Stockton <reply1209@merlyn.demon.co.uk>
Date2012-02-28 22:00 +0000
Message-ID<CESvYCJ95UTPFw+M@invalid.uk.co.demon.merlyn.invalid>
In reply to#12201
In comp.lang.javascript message <e8bddc62-6b7f-4463-b0bb-51912eeca4ef@z3
1g2000vbt.googlegroups.com>, Mon, 27 Feb 2012 07:30:02, Asen Bozhilov
<asen.bozhilov@gmail.com> posted:

>Archos wrote:
>> Is there any way to know if an array is empty? Without using its
>> length method?
>
>The array is applicable when you need sequenced collection. In
>sequenced collection the `lenght' property is sufficed to check for
>emptiness.

Only if the indexes are all in the range from zero to some non-negative
integer.

A = [] ;                                // empty array
A[undefined] = NaN ;                    // non-empty array
for (J in A) alert(J + " " + A[J]) ;    // one alert :   undefined NaN
alert(A.length) ;                       // shows a zero

-- 
 (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk  BP7, Delphi 3 & 2006.
   <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
   <http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
   NOT <http://support.codegear.com/newsgroups/>: news:borland.* Guidelines

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


#12264

FromGene Wirchenko <genew@ocis.net>
Date2012-02-28 17:20 -0800
Message-ID<opuqk7hsm5bkmp0b14shc14vmv3di8honh@4ax.com>
In reply to#12260
On Tue, 28 Feb 2012 22:00:29 +0000, Dr J R Stockton
<reply1209@merlyn.demon.co.uk> wrote:

>In comp.lang.javascript message <e8bddc62-6b7f-4463-b0bb-51912eeca4ef@z3
>1g2000vbt.googlegroups.com>, Mon, 27 Feb 2012 07:30:02, Asen Bozhilov
><asen.bozhilov@gmail.com> posted:
>
>>Archos wrote:
>>> Is there any way to know if an array is empty? Without using its
>>> length method?
>>
>>The array is applicable when you need sequenced collection. In
>>sequenced collection the `lenght' property is sufficed to check for
>>emptiness.
>
>Only if the indexes are all in the range from zero to some non-negative
>integer.
>
>A = [] ;                                // empty array
>A[undefined] = NaN ;                    // non-empty array
>for (J in A) alert(J + " " + A[J]) ;    // one alert :   undefined NaN
>alert(A.length) ;                       // shows a zero

     The emptiness check could apparently be

var fEmpty=true;
for (Item in theArray)
  {
  fEmpty=false;
  break;
  }

I checked this, and it works, but is there a better way?  Entering a
loop and breaking out of it unconditionally is perverse.

     You could join the array and check against an empty string, but
that could be expensive with a large array.  toString() is similar.

     I thought that maybe trying to Array.pop(), but if there is no
element, then undefined is returned, and that could be a valid array
element.  I expect the same is so for Array.shift().

Sincerely,

Gene Wirchenko

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


#12270

FromAsen Bozhilov <asen.bozhilov@gmail.com>
Date2012-02-29 01:23 -0800
Message-ID<3762265.125.1330507383456.JavaMail.geo-discussion-forums@ynil17>
In reply to#12264
Gene Wirchenko написа:

>      The emptiness check could apparently be
> 
> var fEmpty=true;
> for (Item in theArray)
>   {
>   fEmpty=false;
>   break;
>   }
> 
> I checked this, and it works, but is there a better way?  Entering a
> loop and breaking out of it unconditionally is perverse.

Actually this is not a check for an emptiness. That checks for enumerable properties, which is different than the OP's question.   

Firstable we have to consider what is an empty object in ECMAScript. By default every native object inherits from `Object.prototype` and the object is not empty.  The only one exception is: 

var empty = Object.create(null);  

With `Object.create` you can create proto-less object, which does not inherit properties from anywhere. That object can be consider as an empty object.

ECMAScript 5 provides API for manipulating the internal states of the properties. We are able to create non-enumerable properties, which means the checking for emptiness with `for-in` will not return correct result.  

As I wrote, there are no reasons to use sparse arrays or arrays with no integer properties. In both cases a native object would be sufficient. The arrays are usable when they are sequenced collection. 
When I expect somewhere an array in my program I do the following to extract its elements. 

for (var i = 0, len = arr.length; i < len; i++) {
    // do something with arr[i] element
} 

By this, checking for emptiness of array is achieved via its `length' property. This is the simpliest and efficient way. 

Checking for emptiness of sparse arrays and objects could be achived with custom setter and delete methods. For examle if you use proxy object you can use counter for the properties. In the setter if there is not the used property you should increment the counter and in delete the opposite operation.    



  

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


#12309

FromGene Wirchenko <genew@ocis.net>
Date2012-02-29 14:12 -0800
Message-ID<mh8tk7l3t3sq6q26cmgvbb2migv5ocelni@4ax.com>
In reply to#12270
On Wed, 29 Feb 2012 01:23:03 -0800 (PST), Asen Bozhilov
<asen.bozhilov@gmail.com> wrote:

>Gene Wirchenko ??????:
>
>>      The emptiness check could apparently be
>> 
>> var fEmpty=true;
>> for (Item in theArray)
>>   {
>>   fEmpty=false;
>>   break;
>>   }
>> 
>> I checked this, and it works, but is there a better way?  Entering a
>> loop and breaking out of it unconditionally is perverse.
>
>Actually this is not a check for an emptiness. That checks for enumerable properties, which is different than the OP's question.   

     If one is using an array associatively, then it could have no
numbered elements but still be non-empty as in:
          var theArray=[];
          // theArray is empty at this point per my check above.
          theArray["example"]="This is my example.";
          // theArray is not empty now per my check above.

[snip]

Sincerely,

Gene Wirchenko

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


#12333

FromDr J R Stockton <reply1209@merlyn.demon.co.uk>
Date2012-03-01 21:20 +0000
Message-ID<pvrVCIOZg+TPFw4Y@invalid.uk.co.demon.merlyn.invalid>
In reply to#12264
In comp.lang.javascript message <opuqk7hsm5bkmp0b14shc14vmv3di8honh@4ax.
com>, Tue, 28 Feb 2012 17:20:30, Gene Wirchenko <genew@ocis.net> posted:

>
>     You could join the array and check against an empty string, but
>that could be expensive with a large array.

And expensive code is particularly bad value if it does not work.

In current Firefox :
        A = []
        A[4.67] = 66
        A.length          // 0
        A.join()          // blank
        A.join().length   // 0
        A[467/100]        // 66

When testing for emptiness, the first thing to do is to define the sort
of item that one wants to test for the absence of.

My favourite tea-cup might be considered to be empty - it is upside-
down.  But it is almost full of air.  It also contains about half a
milk-bottle, itself empty - of milk but not of air.  The air itself is
almost empty; only about 0.1% of it is atoms.  The atoms are even
emptier.

-- 
 (c) John Stockton, nr London UK.  ?@merlyn.demon.co.uk  DOS 3.3 6.20 ; WinXP.
   Web  <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
   PAS EXE TXT ZIP via  <http://www.merlyn.demon.co.uk/programs/00index.htm>
   My DOS  <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

[toc] | [prev] | [standalone]


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


csiph-web