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

Y

Yansky

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 };
 
B

Bart Van der Donck

Yansky said:
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,
 
Y

Yansky

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.
 
B

Bart Van der Donck

Yansky said:
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.
 
Y

Yansky

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

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


Ah ok thanks, that works great. :)
 
Y

Yansky

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

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


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.
 
Y

Yansky

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

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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top