Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Combining object arrays

Reply
Thread Tools

Combining object arrays

 
 
mike
Guest
Posts: n/a
 
      09-14-2005
If I have 2 object arrays like:

var txtobj = theform.getElementsByTagName("input");
var selobj = theform.getElementsByTagName("select");

and i want to iterate over them I'd like to combine them and then
iterate over them.

so I would do something like this below, but that doesn't look right.
var bothobj = txtobj+selobj;

for( var i=0; i<bothobj.length; i++ )
{
do something....
}

Any thought?

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      09-14-2005
mike said the following on 9/14/2005 4:37 PM:

> If I have 2 object arrays like:
>
> var txtobj = theform.getElementsByTagName("input");
> var selobj = theform.getElementsByTagName("select");
>
> and i want to iterate over them I'd like to combine them and then
> iterate over them.
>
> so I would do something like this below, but that doesn't look right.
> var bothobj = txtobj+selobj;
>
> for( var i=0; i<bothobj.length; i++ )
> {
> do something....
> }
>
> Any thought?


Test it and see.

..concat()

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
 
Reply With Quote
 
 
 
 
Mick White
Guest
Posts: n/a
 
      09-14-2005
mike wrote:

> If I have 2 object arrays like:
>
> var txtobj = theform.getElementsByTagName("input");
> var selobj = theform.getElementsByTagName("select");


Bad!!
var txtobj = document.theform.getElementsByTagName("input")

But why not use DOM 0?

function checkInputsAndSelects(form){
var f=form.length;
while(f--){
if(form[f].type.toLowerCase()=="input"){
// do stuff with input
}
if(form[f].type.toLowerCase()=="select"){
// do stuff with select
}
}
}

Mick

[...]
 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      09-14-2005
Mick,

Why is this bad?

var txtobj = document.theform.getElementsByTagName("input")

Is it because you think it only gets <input ..> but not <INPUT ...>

Mike

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      09-14-2005
mike wrote:

> Mick,
>
> Why is this bad?
>
> var txtobj = document.theform.getElementsByTagName("input")
>
> Is it because you think it only gets <input ..> but not <INPUT ...>
>


var txtobj = theform.getElementsByTagName("input");

missing "document" reference...

Will work in IE, though.

Mick
 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      09-14-2005
i had already defined

var theform = document.update;

so ... it is ok then ... ?

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-15-2005
mike wrote:
> If I have 2 object arrays like:
>
> var txtobj = theform.getElementsByTagName("input");
> var selobj = theform.getElementsByTagName("select");
>
> and i want to iterate over them I'd like to combine them and then
> iterate over them.
>
> so I would do something like this below, but that doesn't look right.
> var bothobj = txtobj+selobj;


No, it's not right. getElementsByTagName returns an HTML collection,
not an array. Collections have some array-like properties, e.g. length,
but have none of an Array's methods.

>
> for( var i=0; i<bothobj.length; i++ )
> {
> do something....
> }


To concatenate collections, you could create a concatenation function
that adds the elements of a collection to an array[1]:

function concatCollections() {
var c, k, j, i = arguments.length;
var a = [];
for ( j=0; j<i; j++ ) {
c = arguments[j];
h = c.length;
for ( k=0; k<h; k++ ){
a.push(c[k]);
}
}
return a;
}

And once you've created the collections, call the above function:

var arrayOfElements = concatCollections(txtobj, selobj);

But this seems a waste of time. Whatever function that is going to
iterate over the array could accept multiple arguments and iterate over
collections instead.

Unless there are some array methods you'd like to use.

[1] Push is not supported in very old browsers (it was introduced in
JavaScript 1.2, works in Netscape 3+ and IE 5+ I think), it's pretty
simple to create your own push function if required.
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array ush>


--
Rob
 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      09-15-2005
mike wrote:

> i had already defined
>
> var theform = document.update;
>
> so ... it is ok then ... ?
>

Your original post:

<QUOTE>
If I have 2 object arrays like:

var txtobj = theform.getElementsByTagName("input");
var selobj = theform.getElementsByTagName("select");

[...]
</QUOTE>

Missing "document" reference.
Mick
 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      09-15-2005
But this seems a waste of time. Whatever function that is going to
iterate over the array could accept multiple arguments and iterate over

collections instead.

I agree. thanks though for your explanation and collection code.

Mike

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      09-15-2005
mike said:
>
>If I have 2 object arrays like:
>
>var txtobj = theform.getElementsByTagName("input");
>var selobj = theform.getElementsByTagName("select");
>
>and i want to iterate over them I'd like to combine them and then
>iterate over them.


You're probably handling the input's and the select's differently
anyway, so why bother with pulling them out and concatinating them?

for (i=0;i<theform.elements.length;i++) {
if (theform.elements[i].type=="input") {
} else if (...) {
...
}
}



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Trouble combining arrays in a table ferraro.joseph@gmail.com Javascript 1 07-28-2006 03:40 AM
Combining three arrays of hashes into one? Keith Lee Perl Misc 8 02-22-2006 12:53 AM
combining 2 multi-dim arrays mike Javascript 3 01-12-2006 06:17 PM
combining two arrays tgiles@gmail.com Perl Misc 8 03-03-2005 06:17 PM



Advertisments