Adding elements with concatination to an Array

Discussion in 'Javascript' started by JS, May 29, 2005.

  1. JS

    JS Guest

    I am trying to add an element to an array like this:

    var ty = [99, 100];
    zz = "ty";
    var ab = null;

    zz+="[23]";

    ab = eval(zz);
    document.write(ab.length);

    But it don't work. What am I doing wrong??
     
    JS, May 29, 2005
    #1
    1. Advertising

  2. On 29/05/2005 21:53, JS wrote:

    > I am trying to add an element to an array like this:
    >
    > var ty = [99, 100];
    > zz = "ty";
    > var ab = null;
    >
    > zz+="[23]";
    >
    > ab = eval(zz);


    [snip]

    Simplifying that code, you get:

    var ty = [99, 100],
    ab = eval('ty[23]');

    which can be simplifier further to:

    var ty = [99, 100],
    ab = ty[23];

    Basically, you create an array with two elements that contain the
    numbers 99 and 100, then attempt to assign the 23th element (which is
    undefined) to the variable, ab.

    To append a value to an array, you can use the push method:

    ty.push(23);

    This accepts any number of arguments and adds them all in left-to-right
    order. You can also concatenate two arrays together with the concat method:

    ty.concat([23]);

    Mike


    Note that the push method isn't implements by JScript versions prior to
    5.5 (and therefore, usually IE versions prior to 5.5) so it may require
    emulation:

    if('function' != typeof Array.prototype.push) {
    Array.prototype.push = function() {
    var i = 0, j = this.length, n = arguments.length;

    while(i < n) {this[j++] = arguments[i++];}
    return j;
    };
    }

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, May 29, 2005
    #2
    1. Advertising

  3. JS

    JS Guest

    "Michael Winter" <> skrev i en meddelelse
    news:daqme.41971$...
    > On 29/05/2005 21:53, JS wrote:
    >
    > > I am trying to add an element to an array like this:
    > >
    > > var ty = [99, 100];
    > > zz = "ty";
    > > var ab = null;
    > >
    > > zz+="[23]";
    > >
    > > ab = eval(zz);

    >
    > [snip]
    >
    > Simplifying that code, you get:
    >
    > var ty = [99, 100],
    > ab = eval('ty[23]');
    >
    > which can be simplifier further to:
    >
    > var ty = [99, 100],
    > ab = ty[23];
    >



    But I would like to use the concatination method, just to learn how it
    works. I was told that it should be possible to convert an array into a
    string that still holds the elements:

    var ty = [99, 100];
    zz = "ty";

    Then through zz, it should be possible to add elements to ty:

    zz+="[23]";

    that can be viewed through "ab":

    ab = eval(zz);
    document.write(ab.length);

    I know it seems silly, but I would just like to know what I am doing wrong,
    because this method should work.
     
    JS, May 29, 2005
    #3
  4. "JS" <> writes:

    >> > zz = "ty";

    ....
    >> > zz+="[23]";
    >> >
    >> > ab = eval(zz);



    > But I would like to use the concatination method, just to learn how it
    > works.


    Concatentaing strings works fine, but it was never a good way to access
    values.

    > I was told that it should be possible to convert an array into a
    > string that still holds the elements:


    That, in itself, sounds ... misunderstood. Who told you that? If
    you have a link, I'd like to read it myself :)

    You are not turning a variable into a string, you merely make a string
    containing a variable name. Might as well let the variable point to
    the value instead of the name.

    > var ty = [99, 100];
    > zz = "ty";


    > Then through zz, it should be possible to add elements to ty:
    >
    > zz+="[23]";


    Appending the string "[23]" does not add any elements to anyting, but
    it does add characters to the string. If what you really want is
    to add the value "23" to the array currently referenced by the variable
    "ty", then the quickest way is:
    ty.push(23)
    or, for older browsers without a "push" method;
    ty[ty.length] = 23;

    > that can be viewed through "ab":
    >
    > ab = eval(zz);
    > document.write(ab.length);


    Here it seems you are assuming that evaluating the string will both
    1) append 23 to the array
    2) evaluate to a reference to that array.
    It does neither.

    The string you end up with is "ty[23]", there is nothing magic about string
    concatenation, so you get exactly what you ask for :)

    When evaluated, using "eval", it does exactly what the expression
    ty[23]
    would do: look up the value at index 23 of the array referenced by "ty".
    Since there is no such element, the resulting value is "undefined". There
    is no assignment in that expression, so no change to any array.

    > I know it seems silly, but I would just like to know what I am doing wrong,
    > because this method should work.


    No, it shouldn't.

    Whatever you do using "eval" can also be done without it, and 99.9% of
    the time the alternative is shorter, safer, and less likely to blow up
    in your face.

    I could write something that would append a value to an array, using
    eval, but it has so many shortcommings that I fear writing it out,
    because someone might use it.


    /L 'ab=eval("["+ty+",23]");//DON'T EVER DO THIS!'
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, May 29, 2005
    #4
  5. JS

    Lee Guest

    JS said:

    >var ty = [99, 100];
    >zz = "ty";
    >
    >Then through zz, it should be possible to add elements to ty:
    >
    >zz+="[23]";
    >
    >that can be viewed through "ab":


    At this point, zz="ty[23]"

    >ab = eval(zz);


    Since ty doesn't contain an element with index 23,
    ab is now undefined.

    You could assign a value to it:

    eval(zz+"=101");
    alert(ty[23]);

    As a rule of thumb, if you find yourself using eval(), you've
    almost certainly overlooked a better way to accomplish your goal.
     
    Lee, May 29, 2005
    #5
  6. JS wrote:
    > "Michael Winter" <> skrev i en
    > meddelelse news:daqme.41971$...
    >> On 29/05/2005 21:53, JS wrote:

    <snip>
    >>> zz+="[23]";
    >>>
    >>> ab = eval(zz);

    >>
    >> [snip]
    >>
    >> Simplifying that code, you get:
    >>
    >> var ty = [99, 100],
    >> ab = eval('ty[23]');
    >>
    >> which can be simplifier further to:
    >>
    >> var ty = [99, 100],
    >> ab = ty[23];

    >
    > But I would like to use the concatination method,


    Mike just showed you how to use the - concat - method of Arrays, your
    original code uses the compound assignment operator - += -, which is not
    necessarily a concatenation operator as its action depends on the type
    of its operands. And when it does concatenation it is string
    concatenation that it does.

    > just to learn how it works.


    It is unlikely that a compound assignment operator will 'work' in this
    context.

    > I was told that it should be possible to convert an
    > array into a string that still holds the elements:


    Whoever told you that is not a useful source of programming advice. You
    might just about get away with that sort of action if all of the
    elements are numeric or boolean, with considerable additional string
    manipulation.

    But you should observe the general javascript programming advice; if you
    think you need to use the - eval - function then the odds are that your
    code design is fundamentally wrong.

    > var ty = [99, 100];
    > zz = "ty";
    >
    > Then through zz, it should be possible to add elements to ty:
    >
    > zz+="[23]";


    As you are going to eval this you are building a string containing
    javascript source code. That string is "ty[23]", which is a bracket
    notation property accessor (something that you _never_ need to construct
    as a string and then eval).

    > that can be viewed through "ab":
    >
    > ab = eval(zz);


    The property accessor references the element of the array with the index
    23, but the array only has two elements so the "23" element is
    undefined.

    > document.write(ab.length);


    And assigning the value of an element in an array to a variable does not
    alter the length of an array.

    > I know it seems silly,


    Absolutly.

    > but I would just like to know what I am doing
    > wrong,


    Apparently you are listening to bad advice, ignoring good advice and
    failing to RTFM.

    > because this method should work.


    It does work, it is just that what it does (and should do) bares no
    relationship with your apparent intent.

    Richard.
     
    Richard Cornford, May 29, 2005
    #6
  7. JS

    RobG Guest

    Lee wrote:
    > JS said:
    >
    >
    >>var ty = [99, 100];
    >>zz = "ty";
    >>
    >>Then through zz, it should be possible to add elements to ty:
    >>
    >>zz+="[23]";
    >>
    >>that can be viewed through "ab":

    >
    >
    > At this point, zz="ty[23]"
    >
    >
    >>ab = eval(zz);

    >
    >
    > Since ty doesn't contain an element with index 23,
    > ab is now undefined.
    >
    > You could assign a value to it:
    >
    > eval(zz+"=101");
    > alert(ty[23]);
    >


    Without commenting on 'eval', this puts the value '101' at index 23 of
    the array, effectively adding 21 empty elements. 'ty' is now:

    [99,100,,,,,,,,,,,,,,,,,,,,,,101]

    The simple way to append elements is with:

    ty.push(23);

    the alternative where push is not supported is:

    ty[ty.length] = 23;

    which will result in ty looking like:

    [99,100,23]

    Which may be what the OP was after (or not...).


    --
    Rob
     
    RobG, May 30, 2005
    #7
  8. JS

    Lee Guest

    RobG said:
    >
    >Lee wrote:
    >> JS said:
    >>
    >>
    >>>var ty = [99, 100];
    >>>zz = "ty";
    >>>
    >>>Then through zz, it should be possible to add elements to ty:
    >>>
    >>>zz+="[23]";
    >>>
    >>>that can be viewed through "ab":

    >>
    >>
    >> At this point, zz="ty[23]"
    >>
    >>
    >>>ab = eval(zz);

    >>
    >>
    >> Since ty doesn't contain an element with index 23,
    >> ab is now undefined.
    >>
    >> You could assign a value to it:
    >>
    >> eval(zz+"=101");
    >> alert(ty[23]);
    >>

    >
    > Without commenting on 'eval', this puts the value '101' at index 23 of
    > the array, effectively adding 21 empty elements. 'ty' is now:
    >
    > [99,100,,,,,,,,,,,,,,,,,,,,,,101]
    >
    > The simple way to append elements is with:
    >
    > ty.push(23);
    >
    > the alternative where push is not supported is:
    >
    > ty[ty.length] = 23;
    >
    > which will result in ty looking like:
    >
    > [99,100,23]
    >
    > Which may be what the OP was after (or not...).


    Ok, I saw +="[23]" as trying to index location 23, but I agree now
    that he was trying to append an array literal.
     
    Lee, May 30, 2005
    #8
  9. JS

    RobG Guest

    Lee wrote:
    > RobG said:
    >
    >>Lee wrote:
    >>
    >>>JS said:
    >>>
    >>>
    >>>
    >>>>var ty = [99, 100];
    >>>>zz = "ty";
    >>>>
    >>>>Then through zz, it should be possible to add elements to ty:
    >>>>
    >>>>zz+="[23]";
    >>>>
    >>>>that can be viewed through "ab":
    >>>
    >>>
    >>>At this point, zz="ty[23]"
    >>>
    >>>
    >>>
    >>>>ab = eval(zz);
    >>>
    >>>
    >>>Since ty doesn't contain an element with index 23,
    >>>ab is now undefined.
    >>>
    >>>You could assign a value to it:
    >>>
    >>> eval(zz+"=101");
    >>> alert(ty[23]);
    >>>

    >>
    >> Without commenting on 'eval', this puts the value '101' at index 23 of
    >> the array, effectively adding 21 empty elements. 'ty' is now:
    >>
    >> [99,100,,,,,,,,,,,,,,,,,,,,,,101]
    >>
    >> The simple way to append elements is with:
    >>
    >> ty.push(23);
    >>
    >> the alternative where push is not supported is:
    >>
    >> ty[ty.length] = 23;
    >>
    >> which will result in ty looking like:
    >>
    >> [99,100,23]
    >>
    >> Which may be what the OP was after (or not...).

    >
    >
    > Ok, I saw +="[23]" as trying to index location 23, but I agree now
    > that he was trying to append an array literal.
    >


    What you had is probably a valid interpretation of the OP's request,
    it's difficult to determine a poster's requirements in most cases - I'm
    off-base perhaps 50% of the time (some may suggest a higher number...).
    8-p


    --
    Rob
     
    RobG, May 30, 2005
    #9
    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. Jakob Simon-Gaarde

    Preprocessor concatination of defines

    Jakob Simon-Gaarde, Jul 1, 2004, in forum: C++
    Replies:
    4
    Views:
    445
    Jack Klein
    Jul 2, 2004
  2. JChG
    Replies:
    1
    Views:
    246
    Steven D'Aprano
    Apr 19, 2010
  3. Noah Roberts
    Replies:
    5
    Views:
    763
    James Kanze
    Jan 12, 2011
  4. Rajika Somasiri

    String concatination

    Rajika Somasiri, Feb 9, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    138
    Tom Kiefer
    Feb 9, 2004
  5. Rajika Somasiri

    String concatination

    Rajika Somasiri, Feb 9, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    113
    Anusha Nanayakkara
    Feb 23, 2004
Loading...

Share This Page