How to add items to an object in a for loop?

Discussion in 'Javascript' started by Yansky, Oct 14, 2007.

  1. Yansky

    Yansky Guest

    I'm trying to collect links on a page and then store each link's
    textContent and the reference to the link itself in an object, but I'm
    not sure how I go about adding new items to the object.

    This is what I have so far:

    var meh = {};

    var getas = document.getElementsByTagName('a');

    for(var i=0;i<getas.length;i++){

    meh.push({getas.textContent: getas});

    }

    I know I can't use push() to add to the "meh" object, but that's
    basically what I want to do. I'd like to end up with:

    meh = { "yahoo" : dom reference to link,
    "google" : dom reference to link,
    "msn" : dom reference to link };
     
    Yansky, Oct 14, 2007
    #1
    1. Advertising

  2. Yansky wrote:

    > I'm trying to collect links on a page and then store each link's
    > textContent and the reference to the link itself in an object, but I'm
    > not sure how I go about adding new items to the object.
    >
    > This is what I have so far:
    >
    > var meh = {};
    > var getas = document.getElementsByTagName('a');
    > for(var i=0;i<getas.length;i++){
    > meh.push({getas.textContent: getas});


    Change this last line into

    meh[getas.innerHTML] = getas;

    > }
    >
    > I know I can't use push() to add to the "meh" object, but that's
    > basically what I want to do. I'd like to end up with:
    >
    > meh = { "yahoo" : dom reference to link,
    > "google" : dom reference to link,
    > "msn" : dom reference to link };


    Hope this helps,

    --
    Bart
     
    Bart Van der Donck, Oct 14, 2007
    #2
    1. Advertising

  3. Yansky

    Yansky Guest

    Oh wait, I figured it out:

    var meh = {};

    var getas = document.getElementsByTagName('a');

    for(var i=0;i<getas.length;i++){

    var theText = getas.textContent;

    meh[theText] = getas;

    }

    But now I'm not sure how I can access an item based on a number (I
    want to iterate through items in the object). e.g. meh[0] doesn't
    work.



    On Oct 14, 8:40 pm, Yansky <> wrote:
    > I'm trying to collect links on a page and then store each link's
    > textContent and the reference to the link itself in an object, but I'm
    > not sure how I go about adding new items to the object.
    >
    > This is what I have so far:
    >
    > var meh = {};
    >
    > var getas = document.getElementsByTagName('a');
    >
    > for(var i=0;i<getas.length;i++){
    >
    > meh.push({getas.textContent: getas});
    >
    > }
    >
    > I know I can't use push() to add to the "meh" object, but that's
    > basically what I want to do. I'd like to end up with:
    >
    > meh = { "yahoo" : dom reference to link,
    > "google" : dom reference to link,
    > "msn" : dom reference to link };
     
    Yansky, Oct 14, 2007
    #3
  4. Yansky wrote:

    > var meh = {};
    > var getas = document.getElementsByTagName('a');
    > for(var i=0;i<getas.length;i++){
    > var theText = getas.textContent;
    > meh[theText] = getas;
    > }
    >
    > But now I'm not sure how I can access an item based on a number (I
    > want to iterate through items in the object). e.g. meh[0] doesn't
    > work.


    for (var i in meh)
    alert(i + ': ' + meh);

    You can not access an object using numbers, since properties are
    unsorted and unnumbered.

    --
    Bart
     
    Bart Van der Donck, Oct 14, 2007
    #4
  5. Yansky

    Yansky Guest

    On Oct 14, 9:08 pm, Bart Van der Donck <> wrote:
    >
    > for (var i in meh)
    > alert(i + ': ' + meh);
    >
    > You can not access an object using numbers, since properties are
    > unsorted and unnumbered.
    >
    > --
    > Bart


    Ah ok thanks, that works great. :)
     
    Yansky, Oct 14, 2007
    #5
  6. Yansky

    Yansky Guest

    On Oct 14, 9:08 pm, Bart Van der Donck <> wrote:
    >
    > for (var i in meh)
    > alert(i + ': ' + meh);
    >
    > You can not access an object using numbers, since properties are
    > unsorted and unnumbered.
    >
    > --
    > Bart


    I'm still having some problems. Here's what I'm trying to do;

    I'm trying to sort links on a page alphabetically by their text
    content. The problem is, I can't figure out how to sort them once I
    put them in the associative array object thingo (i.e. meh).

    var meh = {};

    var getas = document.getElementsByTagName('a');­

    for(var i=0;i<getas.length;i++){

    meh[getas.textContent] = getas;

    }

    meh.sort() doesn't work, so I'm trying your method to iterate over
    each item:


    for (var i in meh){

    var nextItem = i++;

    if( i.charAt(0) < nextItem.charAt(0) ){

    //do something

    }
    else if( i.charAt(0) > nextItem.charAt(0) ){

    //do something

    }
    else{

    //do something

    }

    }

    The problem is, I don't know how to sort the items after I've compared
    them. Also, I'm not sure if var nextItem = i++; will work since as you
    say, I can't access an object using numbers.
     
    Yansky, Oct 14, 2007
    #6
  7. Yansky

    Yansky Guest

    On Oct 14, 10:27 pm, Yansky <> wrote:
    > On Oct 14, 9:08 pm, Bart Van der Donck <> wrote:
    >
    >
    >
    > > for (var i in meh)
    > > alert(i + ': ' + meh);

    >
    > > You can not access an object using numbers, since properties are
    > > unsorted and unnumbered.

    >
    > > --
    > > Bart

    >
    > I'm still having some problems. Here's what I'm trying to do;
    >
    > I'm trying to sort links on a page alphabetically by their text
    > content. The problem is, I can't figure out how to sort them once I
    > put them in the associative array object thingo (i.e. meh).
    >
    > var meh = {};
    >
    > var getas = document.getElementsByTagName('a');­
    >
    > for(var i=0;i<getas.length;i++){
    >
    > meh[getas.textContent] = getas;
    >
    > }
    >
    > meh.sort() doesn't work, so I'm trying your method to iterate over
    > each item:
    >
    > for (var i in meh){
    >
    > var nextItem = i++;
    >
    > if( i.charAt(0) < nextItem.charAt(0) ){
    >
    > //do something
    >
    > }
    > else if( i.charAt(0) > nextItem.charAt(0) ){
    >
    > //do something
    >
    > }
    > else{
    >
    > //do something
    >
    > }
    >
    > }
    >
    > The problem is, I don't know how to sort the items after I've compared
    > them. Also, I'm not sure if var nextItem = i++; will work since as you
    > say, I can't access an object using numbers.


    I figured it out. I need meh to be a regular array and then store and
    object as an array item.
     
    Yansky, Oct 14, 2007
    #7
    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. Gilles Kuhn
    Replies:
    0
    Views:
    440
    Gilles Kuhn
    Sep 15, 2003
  2. Bart Van Hemelen
    Replies:
    2
    Views:
    4,674
    Bart Van Hemelen
    Sep 4, 2006
  3. tabonni
    Replies:
    1
    Views:
    189
    Bob Barrows [MVP]
    Aug 20, 2004
  4. tabonni
    Replies:
    4
    Views:
    231
    kaeli
    Aug 23, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    382
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page