Forum Moderators: open

Message Too Old, No Replies

Filtering Duplicates

         

switchtone

4:33 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



Hi everyone,

I'm stuck with a slight problem that I hope someone can help me with. I have a very large array (short version below) that I'm using to populate a series of dropdowns with. The full thing is working exactly as I want, but the way it's set up, the first set is showing the same item multiple times. So in the example below, I see "Red" 4 times.

Is there any way to filter this out in my loop so each unique "groupid" only shows up once? I'm currently using jQuery where I can, so I'm open to any solution that could provide as well.


$(document).ready(function() {
var units = [
{groupid:"Red", zone:"Section 1"},
{groupid:"Red", zone:"Section 2"},
{groupid:"Red", zone:"Section 3"},
{groupid:"Red", zone:"Section 4"},
{groupid:"Blue", zone:"Section 2"},
{groupid:"Blue", zone:"Section 4"},
{groupid:"Blue", zone:"Section 6"},
{groupid:"Green", zone:"Section 5"},
{groupid:"Green", zone:"Section 6"}
]
for (var unitlist = 0; unitlist< units.length; unitlist++) {
buildList(document.unit_list.groups, units[unitlist].groupid, units[unitlists].groupid, "");
}
});
function buildList(selectbox, value, text ) {
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

Thanks in advance for any help/advice!

MarkFilipak

5:24 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



I'm mystified by your code. Is it javascript?

What is $(document) ?
What does $(document).ready(function() { ... }); do?
What is the value of unit_list ?
What is the value of unit_list.groups ?

[edited by: MarkFilipak at 5:25 pm (utc) on Mar. 5, 2008]

switchtone

5:46 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



Yes, it's javascript..lol The $(document).ready(function() is a jQuery function that sorta works as a replacement to onload. Do a Google search for a detailed write up on that. Aside from that, it's nothing fancy.

"document.unit_list.groups" is referencing the form fields.

MarkFilipak

6:01 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



If jQuery has a discussion forum, you may want to submit your problem there. I don't know what "the first set" is and I don't know what document.unit_list is all about, so I guess I can't help you. Ciao.

switchtone

6:14 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



..except, this isn't written using jQuery, just how it loads. Thanks for trying, though. :-)

Fotiman

7:23 pm on Mar 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




var currentgroup = "";
for (var unitlist = 0; unitlist< units.length; unitlist++) {
if (currentgroup != units[unitlist].groupid) {
currentgroup = units[unitlist].groupid;
buildList(document.unit_list.groups, units[unitlist].groupid, units[unitlists].groupid, "");
}
}

switchtone

7:48 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



Brilliant, Fotiman! That did the trick perfectly. Thank you very much :-)