Array in javascript

Discussion in 'Javascript' started by Leszek, Feb 2, 2006.

  1. Leszek

    Leszek Guest

    Hi.

    Is it possible in javascript to operate on an array without knowing how mamy
    elements it has?
    What i want to do is sending an array to a script, and this script should
    add all values from that array

    Could you show me a little example how to do this?

    Thanks.
    Leszek
     
    Leszek, Feb 2, 2006
    #1
    1. Advertising

  2. Leszek wrote:

    > Is it possible in javascript to operate on an array without knowing how
    > mamy elements it has?


    Of course. This is what the e.g. `length' property of Array objects is for.

    > What i want to do is sending an array to a script, and this
    > script should add all values from that array
    >
    > Could you show me a little example how to do this?


    Depends on what you mean by "sending an array to a script" (which array,
    which script, client-side or server-side, send from where to where?) and by
    "script should add all values from that array" (add from where exactly to
    which?)


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 2, 2006
    #2
    1. Advertising

  3. Leszek

    VK Guest

    Leszek wrote:
    > Hi.
    >
    > Is it possible in javascript to operate on an array without knowing how mamy
    > elements it has?


    JavaScript array is Dynamic Sparse Jagged Array. That can be too much
    of adjectives for one time :) but the first one means that can add new
    elements to array without ReDim / resize() it.

    var myArray = new Array(); // 0 elements
    myArray[0] = 0;
    ....
    ....
    myArray[1000] = 1000;

    About other adjectives (and about JavaScript arrays at whole) you can
    read at:
    <http://www.geocities.com/schools_ring/ArrayAndHash.html>

    > What i want to do is sending an array to a script, and this script should
    > add all values from that array
    > Could you show me a little example how to do this?


    As it was pointed out it depends on how and in what form are you
    getting the original data for your array.
     
    VK, Feb 2, 2006
    #3
  4. Leszek

    Guillaume Guest

    Leszek
    > Is it possible in javascript to operate on an array without knowing how mamy
    > elements it has?


    I don't think, there is no function like 'map' or 'apply'. However, they
    are not difficult to implement because you always know the size of an
    array: array.length;

    --
    My desktop is worth a million of dollars. Put an icon on it.
    http://www.milliondollarscreenshot.com/
     
    Guillaume, Feb 2, 2006
    #4
  5. VK wrote:

    > Leszek wrote:
    >> Is it possible in javascript to operate on an array without knowing how
    >> mamy elements it has?

    >
    > JavaScript array is Dynamic Sparse Jagged Array. That can be too much
    > of adjectives for one time :)


    But I am sure you can invent more just to cover your lack of knowledge and
    understanding of the basics.

    > but the first one means that can add new
    > elements to array without ReDim / resize() it.
    >
    > var myArray = new Array(); // 0 elements
    > myArray[0] = 0;
    > ...
    > ...
    > myArray[1000] = 1000;


    You did not answer the question of the OP _at all_. Figures.

    > About other adjectives (and about JavaScript arrays at whole) you can
    > read at:
    > <http://www.geocities.com/schools_ring/ArrayAndHash.html>


    This text contains a lot of, if not consists mainly of, factually incorrect
    information, presented as being the truth despite of that. Readers are
    strongly recommended to ignore it, to handle all statements of its author
    regarding (proper) software development with extreme care, and to read
    previous discussions on the subject instead.

    <URL:http://groups.google.com/groups?as_q=Array&as_ugroup=comp.lang.javascript&scoring=d&filter=0>
    <URL:http://groups.google.com/groups?as_uauthors=VK&as_ugroup=comp.lang.javascript&scoring=d&filter=0>


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 2, 2006
    #5
  6. Leszek

    Jambalaya Guest

    Leszek wrote:
    > Hi.
    >
    > Is it possible in javascript to operate on an array without knowing how mamy
    > elements it has?


    Yes.

    > What i want to do is sending an array to a script, and this script should
    > add all values from that array
    >
    > Could you show me a little example how to do this?


    This is the smallest example I could write:

    <html><head><title>Little Sum Function</title></head>
    <body><script type="text/javascript">
    function s(a){return Function('return '+a.join('+'))()}
    document.write(s([1,654,2,5,489,51,3851,681,32,5,0]))
    </script></body></html>
     
    Jambalaya, Feb 2, 2006
    #6
  7. Leszek

    VK Guest

    Thomas 'PointedEars' Lahn wrote:
    > VK wrote:
    >
    > > Leszek wrote:
    > >> Is it possible in javascript to operate on an array without knowing how
    > >> mamy elements it has?

    > >
    > > JavaScript array is Dynamic Sparse Jagged Array. That can be too much
    > > of adjectives for one time :)

    >
    > But I am sure you can invent more just to cover your lack of knowledge and
    > understanding of the basics.


    JavaScript array is Dinamic, is Sparse and is Jagged. These three core
    features are important to know to operate properly with arrays and get
    expected results.

    <http://www.geocities.com/schools_ring/ArrayAndHash.html> is the only
    one known (to me at least) resource there all three features along with
    other information would be named and illustrated properly: all within
    the same page. Unfortunately (after careful reading and searching) this
    is still the only resource I can endorse for JavaScript array
    information.

    It should be updated though by removing some no so relevant part at the
    bottom and by adding new array methods from JavaScript 1.6 (Firefox 1.5)
     
    VK, Feb 2, 2006
    #7
  8. Jambalaya wrote:

    > Leszek wrote:
    >> What i want to do is sending an array to a script, and
    >> this script should add all values from that array
    >>
    >> Could you show me a little example how to do this?

    >
    > This is the smallest example I could write:
    >
    > <html>
    > <head><title>Little Sum Function</title></head>
    > <body><script type="text/javascript">
    > function s(a){return Function('return '+a.join('+'))()}
    > document.write(s([1,654,2,5,489,51,3851,681,32,5,0]))
    > </script></body></html>


    Nice[1] :)

    Not shorter, but Valid:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"><head><meta
    http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
    <title>Little Adder</title></head><script
    type="text/javascript">document.write(Function("return "
    +[1,654,2,5,489,51,3851,681,32,5,0].join("+"))());</script>


    Regards,
    PointedEars
    ___________
    [1] especially because there is no evil[tm] eval()
     
    Thomas 'PointedEars' Lahn, Feb 2, 2006
    #8
  9. Leszek

    Evertjan. Guest

    Jambalaya wrote on 02 feb 2006 in comp.lang.javascript:

    > This is the smallest example I could write:
    >
    > <html><head><title>Little Sum Function</title></head>
    > <body><script type="text/javascript">
    > function s(a){return Function('return '+a.join('+'))()}
    > document.write(s([1,654,2,5,489,51,3851,681,32,5,0]))
    > </script></body></html>
    >


    <script type="text/javascript">
    alert(eval([1,654,2,5,489,51,3851,681,32,5,0].join('+')));
    </script>

    or for the (eval==evil)-ers:

    <script type="text/javascript">
    var b=0,a=[1,654,2,5,489,51,3851,681,32,5,0],z=a.length;
    while(z)b+=a[--z];alert(b)
    </script>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 2, 2006
    #9
  10. Leszek

    RobG Guest

    Leszek wrote:
    [...]
    > What i want to do is sending an array to a script, and this script should
    > add all values from that array
    >
    > Could you show me a little example how to do this?


    // An array of numbers, some as strings
    var A = [2,'5',8,'2',4];

    // A function that adds all the elements of an array
    function addEm(X)
    {
    var i = X.length;
    var sum = 0;
    while(i--) sum += +X;
    return sum;
    }

    // Show it in action
    alert( addEm(A) );


    If there is any chance that some of the elements might not be numbers,
    then that should be checked before attempting addition:

    var A = [2,'blah','5',8,2,4];

    function addEm(X)
    {
    var x, i = X.length;
    var sum = 0;
    while(i--) {
    x = +X;
    if ( !isNaN(x) ) sum += x;
    }
    return sum;
    }



    --
    Rob
     
    RobG, Feb 2, 2006
    #10
  11. Leszek

    VK Guest

    Ian Collins wrote:
    > I think the previous responses over looked the use of
    > for( var n in array ) {} as a method for iterating over an array.
    >
    > > Could you show me a little example how to do this?
    > >

    >
    > function test( names )
    > {
    > for( var name in names )
    > {
    > // do something.
    > }
    > }


    for-in loop has nothing to do with *array*, it's an iterator over
    enumerable properties of object.

    Try this to understand the difference:

    var arr = [1,2,3];
    arr.foo = 'bar';

    for (var i=0; i<arr.length; i++) {
    alert(arr);
    }

    for (var p in arr) {
    alert(p);
    }
     
    VK, Feb 3, 2006
    #11
  12. VK wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> VK wrote:

    <snip>
    >>> JavaScript array is Dynamic Sparse Jagged Array. That
    >>> can be too much of adjectives for one time :)

    >>
    >> But I am sure you can invent more just to cover your lack
    >> of knowledge and understanding of the basics.

    >
    > JavaScript array is Dinamic, is Sparse and is Jagged.


    Can you justify applying the term "jagged" to a javascript array when
    they can only have a single dimension?

    <snip>
    > <http://www.geocities.com/schools_ring/Array ...

    <snip>
    > ... . Unfortunately (after careful reading and searching)
    > this is still the only resource I can endorse for JavaScript
    > array information.

    <snip>

    Your willingness to endorse your own page is not significant (beyond the
    observation that your tendency to always choose the worst option
    available means that your endorsement may be taken as a condemnation by
    reasoning observers). The important consideration is whether it could be
    endorsed by anyone who actually understands javascript. You have tried
    to get such an endorsement from contributors to this group and the
    reactions you got were anything but an endorsement.

    Richard.
     
    Richard Cornford, Feb 3, 2006
    #12
  13. Leszek

    VK Guest

    Richard Cornford wrote:
    > Can you justify applying the term "jagged" to a javascript array when
    > they can only have a single dimension?


    This is exactly the case with JavaScript array wich is a single
    dimension array when looking at it "from the top". But it can have
    other arrays as its elements. Array of arrays is called in the
    programming "jagged array".
    We may of course continue to stay on the position that nothing of
    regular programming is applicable to JavaScript - neither in terms nor
    in technology :)
    In such case array of arrays is called "jagged" everywhere but in
    JavaScript it's just "array of arrays" :)

    Jagged nature of JavaScript array is really important to know and
    understand for say array sorting.

    > Your willingness to endorse your own page is not significant (beyond the
    > observation that your tendency to always choose the worst option
    > available means that your endorsement may be taken as a condemnation by
    > reasoning observers). The important consideration is whether it could be
    > endorsed by anyone who actually understands javascript. You have tried
    > to get such an endorsement from contributors to this group and the
    > reactions you got were anything but an endorsement.


    This article is not perfect and I willing to update it and make it even
    more transparent. I would like to link some reputable public source
    instead from say Mozilla.org or MSDN or IBM.com etc. Unfortunately
    anything of this kind is either incomplete, or confusing or (more
    often) factually wrong.
     
    VK, Feb 3, 2006
    #13
  14. VK wrote:
    > Richard Cornford wrote:
    >> Can you justify applying the term "jagged" to a javascript
    >> array when they can only have a single dimension?

    >
    > This is exactly the case with JavaScript array wich is a single
    > dimension array when looking at it "from the top".


    Javascript has single dimension arrays however you look at them.

    > But it can have other arrays as its elements. Array of arrays
    > is called in the programming "jagged array".


    No, the concept of a jagged array can only be implemented in javascript
    with an array of arrays, but that does not make the javascript array
    jagged. Indeed, the very fact that you must have more than one Array in
    order to implement the concept of 'jagged' means that the term _cannot_
    apply to a javascript Array (singular).

    And "in the programming" a 'jagged array' is not an array of arrays, as
    is easily demonstrated by the Java array, which is a single array that
    is multidimensional and jagged.

    > We may of course continue to stay on the position that nothing
    > of regular programming is applicable to JavaScript - neither
    > in terms nor in technology :)


    We? You are exclusively and personally responsible for your own
    incoherent ramblings.

    > In such case array of arrays is called "jagged" everywhere
    > but in JavaScript it's just "array of arrays" :)


    Everywhere? I think you will find that the term 'jagged' will normally
    be applied with discrimination beyond your comprehension.

    > Jagged nature of JavaScript array is really important to know
    > and understand for say array sorting.


    So it is important to understand that the javascript array is a single
    dimension array and so cannot be 'jagged'.

    >> Your willingness to endorse your own page is not significant
    >> (beyond the observation that your tendency to always choose
    >> the worst option available means that your endorsement may
    >> be taken as a condemnation by reasoning observers). The
    >> important consideration is whether it could be endorsed by
    >> anyone who actually understands javascript. You have tried to
    >> get such an endorsement from contributors to this group and
    >> the reactions you got were anything but an endorsement.

    >
    > This article is not perfect and I willing to update it and make
    > it even more transparent.


    BODY {
    display:none;
    }

    > I would like to link some reputable public source instead
    > from say Mozilla.org or MSDN or IBM.com etc.


    Bad sources of information often try to gain credibility by linking to
    'reputable public sources'. What is up, you cannot find anything on
    these sites that doesn't directly contradict your page?

    > Unfortunately anything of this kind is either incomplete, or
    > confusing or (more often) factually wrong.


    LOL. You own page is incomplete, confusing and factually wrong. That is
    precisely what was said of it when you attempted to get this group to
    endorse it.

    On the other hand, one of the advantages of your page is that some of
    the 'factually wrong' is so obvious that only the complete novice is
    likely to give any credence to the rest of its text.

    Richard.
     
    Richard Cornford, Feb 3, 2006
    #14
  15. Leszek

    VK Guest

    Richard Cornford wrote:
    > same old story


    All arguments has been spelled many times over the last year.
    You are welcome to give array-related advises from your point of view:
    and I reserve the same for mine.

    Eventually only the practice (who's picture allow to create effective
    predictable code) is the way to check a theory. A theory in
    contradiction with the practical experience is worthless IMHO.
     
    VK, Feb 3, 2006
    #15
  16. Leszek

    VK Guest

    Richard Cornford wrote:
    > No, the concept of a jagged array can only be implemented in javascript
    > with an array of arrays, but that does not make the javascript array
    > jagged. Indeed, the very fact that you must have more than one Array in
    > order to implement the concept of 'jagged' means that the term _cannot_
    > apply to a javascript Array (singular).


    Sorry for making addons to my post, but there is a logical (not
    pragrammatical) error in the above reasonning:

    Neither C++ or Java array are *obligated* to be multi-dimensional. In
    any language I can perfectly create a simple single-dimention array.
    Applying your own thesis: "The very fact that you must have more than
    one dimension in order to implement the concept of `multi-dimensional'
    means that the term _cannot_ apply to a C++ array (singular)."

    This logical casus can be solved is we realize that "milti-dimensional"
    vs. "jagged" are applied to the *model potention* and not to a
    particular given array X, Y or Z.
     
    VK, Feb 3, 2006
    #16
  17. VK wrote:
    > Richard Cornford wrote:
    >> No, the concept of a jagged array can only be implemented in
    >> javascript with an array of arrays, but that does not make
    >> the javascript array jagged. Indeed, the very fact that you
    >> must have more than one Array in order to implement the
    >> concept of 'jagged' means that the term _cannot_ apply to a
    >> javascript Array (singular).

    >
    > Sorry for making addons to my post, but there is a logical
    > (not pragrammatical) error in the above reasonning:


    It is partly the fact that you cannot recognise logical errors that
    leads me to question your rationality.

    > Neither C++ or Java array are *obligated* to be multi-dimensional.


    While javascript Arrays cannot be multi-dimensional.

    > In any language I can perfectly create a simple
    > single-dimention array. Applying your own thesis:
    > "The very fact that you must have more than
    > one dimension in order to implement the concept of
    > `multi-dimensional' means that the term _cannot_ apply
    > to a C++ array (singular)."


    In that case the parallel statement is that because you have to have
    more than one dimension before something is multi-dimensional then a
    _dimension_ (singular) cannot be multi-dimensional. That is, the term
    'multi-dimensiton' cannot be applied to a dimension.

    The term is applicable to arrays in certain languages because they have
    the potential to have multiple dimensions, whether the actually do or
    not (and the Java array has the potential to be jagged as well).

    It is like stating that the javascript Array is a sparse array. Actual
    array instances do not need to be sparse (in the sense of having fewer
    than - array.length - 'array index' properties), but the configuration
    of an instance does not alter the nature of the javascript Array.

    > This logical casus can be solved is we realize that
    > "milti-dimensional" vs. "jagged" are applied to the
    > *model potention* and not to a particular given array
    > X, Y or Z.


    The javascript array has no potential to have multiple dimensions and so
    no potential to be 'jagged'. Both concepts can be successfully
    implemented/emulated with javascript but that can not be done with a
    single javascript Array, and so the terms 'multi-dimensional' and
    'jagged' are not applicable as characteristics of a javascript Array.

    Richard.
     
    Richard Cornford, Feb 3, 2006
    #17
  18. VK wrote:
    > Richard Cornford wrote:
    >> same old story

    >
    > All arguments has been spelled many times over the
    > last year.


    Yes they have, and you have presented no reasoned counter argument, only
    the stubborn refusal to pay any attention.

    > You are welcome to give array-related advises from
    > your point of view: and I reserve the same for mine.


    Your ability (and right) to post any nonsense you choose cannot be
    denied. But your should have no expectation of being allowed to do so
    without being criticised for doing so.

    And I am subject to the same consideration. If I posted nonsense,
    falsehoods and bad advice I expect (and even want) to be corrected and
    criticised for doing so. But that doesn't seem to happen often; not
    because I am intrinsically or inherently correct but because in the past
    I have paid attention to such comments and learnt form them.

    Initial ignorance of any subject is inevitable and as people learn they
    make mistakes, but most learn form, instead of repeating, their
    mistakes.

    > Eventually only the practice (who's picture allow to create
    > effective predictable code) is the way to check a theory.


    But you don't write effective predictable computer code. You write a
    chaotic mush half thought-out reasoning and pure voodoo. The last piece
    of 'full working' code you posted to this group earned the epithet "the
    most bizarre and obtuse storage method I've ever witnessed", with good
    reason, and demonstrated a basic lack of understanding of standard
    language constructs.

    > A theory in contradiction with the practical experience
    > is worthless IMHO.


    A theory that is contradicted by reality is false, but it takes some
    familiarity with testing and analysis methodologies to identify a real
    contradiction. That perverse personal mental process that you like to
    label 'logic' seems to significantly restrict your ability to create and
    apply tests, or analyse the results. A fact that should have been
    sufficiently flagged when you posted earlier in the week that your had
    spent two months testing Mozilla browsers and concluded that they did
    not support - document.styleSheets -, when they have done so since their
    beta versions at least.

    The notions that originate in your head are also theories, and the
    'practical' results you achieve with them are so self evidently inferior
    to what can demonstrably be achieved that even you should be questioning
    them, and not at all surprised that nobody else gives you any credence
    at all.

    Richard.
     
    Richard Cornford, Feb 3, 2006
    #18
  19. Leszek

    VK Guest

    Richard Cornford wrote:
    > The javascript array has no potential to have multiple dimensions and so
    > no potential to be 'jagged'. Both concepts can be successfully
    > implemented/emulated with javascript but that can not be done with a
    > single javascript Array, and so the terms 'multi-dimensional' and
    > 'jagged' are not applicable as characteristics of a javascript Array.


    It's too late and I'm too tired to continue this discussion right now.
    But I bookmark this statement till the next array question - where
    you'll have an opportunity to answer it without using words "dimension"
    or "jagged" or "pseudo-dimension" (because pseudo-dimension directly
    leads to "jagged"). From my humble and very possibly wrong point of
    view it will be funny.
     
    VK, Feb 3, 2006
    #19
  20. VK wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> VK wrote:
    >> > Leszek wrote:
    >> >> Is it possible in javascript to operate on an array without knowing
    >> >> how mamy elements it has?
    >> >
    >> > JavaScript array is Dynamic Sparse Jagged Array. That can be too much
    >> > of adjectives for one time :)

    >>
    >> But I am sure you can invent more just to cover your lack of knowledge
    >> and understanding of the basics.

    >
    > JavaScript array is Dinamic, is Sparse and is Jagged.


    Whatever this means to you.

    > [...]
    > <http://www.geocities.com/schools_ring/ArrayAndHash.html> is the only
    > one known (to me at least) resource there all three features along with
    > other information would be named and illustrated properly: [...]


    That you do not understand the correct terms used in public specifications
    and vendors' documentation does not mean that they are bad. That you do
    not understand what was written before does not make your explanations,
    based on your misconceptions, any better than it.

    <URL:http://developer.mozilla.org/js/specs/ecma-262#a-15.4>
    <URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Literals#Array_Literals>
    <URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:predefined_Core_Objects:Array_Object>
    <URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array>
    <URL:http://msdn.microsoft.com/library/en-us/script56/html/08e5f552-0797-4b48-8164-609582fc18c9.asp>
    <URL:http://msdn.microsoft.com/library/en-us/jscript7/html/jsobjarray.asp>


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 3, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    496
    David RF
    Sep 3, 2009
  2. Tom
    Replies:
    3
    Views:
    213
    salsablr
    Dec 20, 2004
  3. Tuan  Bui
    Replies:
    14
    Views:
    476
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  4. VK
    Replies:
    36
    Views:
    651
    Martin Honnen
    Aug 3, 2005
  5. BrianP
    Replies:
    2
    Views:
    287
Loading...

Share This Page