[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1019
  • Last Modified:

loop javascript array ignoring multiple values

I have a javascript array with many of the same values, Im creating a selectbox and I want the duplicate values not to be ignored
0
bdipasquale
Asked:
bdipasquale
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
raj3060Commented:
Did you say you don't want duplicate values not to be loaded in array??

Can you post the code how you are doing it.
0
 
jentulmanCommented:
Can you post the code? there's nothing to go on.
0
 
raj3060Commented:
still confused. Title says that you want to ignore duplicates, and question says something else.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
UnTwstrCommented:
can you give us an example of some of the values & how you are using them?
0
 
bdipasqualeAuthor Commented:
example of array

manufactuersArray[1089]="Ford";
manufactuersArray[1090]="Ford";
manufactuersArray[1091]="Ford";
manufactuersArray[1092]="Ford";
manufactuersArray[1093]="Lincoln";
manufactuersArray[1094]="Ford";
manufactuersArray[1095]="Mercury";
manufactuersArray[1096]="Mercury";
manufactuersArray[1097]="Mercury";
manufactuersArray[1098]="Mercury";
manufactuersArray[1099]="Mercury";
manufactuersArray[1100]="Mercury";
manufactuersArray[1101]="Ford";
manufactuersArray[1102]="Ford";
manufactuersArray[1103]="Ford";
manufactuersArray[1104]="Ford";

what I would like is a select box with ford, Lincoln, mercury
0
 
UnTwstrCommented:
Give this a shot...

<SCRIPT LANGUAGE="JavaScript">
      manufacturersArray = new Array();
      usedArray = new Array();
      manufacturersArray[1089]="Ford";
      manufacturersArray[1090]="Ford";
      manufacturersArray[1091]="Ford";
      manufacturersArray[1092]="Ford";
      manufacturersArray[1093]="Lincoln";
      manufacturersArray[1094]="Ford";
      manufacturersArray[1095]="Mercury";
      manufacturersArray[1096]="Mercury";
      manufacturersArray[1097]="Mercury";
      manufacturersArray[1098]="Mercury";
      manufacturersArray[1099]="Mercury";
      manufacturersArray[1100]="Mercury";
      manufacturersArray[1101]="Ford";
      manufacturersArray[1102]="Ford";
      manufacturersArray[1103]="Ford";
      manufacturersArray[1104]="Ford";
      
      document.write("<SELECT NAME='manufacturer' ID='manufacturer'>");
      for(i=1089; i< (1089 + manufacturersArray.length);i++){
            flagUsed = false;
            if (usedArray.length > 0){
                  for(k=0;k<usedArray.length;k++){
                        if(manufacturersArray[i] ==usedArray[k]){
                              flagUsed = true;
                              break;
                        }
                  }
            }
            if(flagUsed == false){
                  document.write("<OPTION VALUE='" + i + "'>" + manufacturersArray[i]);
                  usedArray[usedArray.length + 1] = manufacturersArray[i];
            }
      }
      document.write("</SELECT>");
      document.write(usedArray);
</SCRIPT>
0
 
jentulmanCommented:
i can give you a solution using the prototype javascript extensions to make life easier.
www.prototypejs.org

manufactuersArray[1089]="Ford";
manufactuersArray[1090]="Ford";
manufactuersArray[1091]="Ford";
manufactuersArray[1092]="Ford";
manufactuersArray[1093]="Lincoln";
manufactuersArray[1094]="Ford";
manufactuersArray[1095]="Mercury";
manufactuersArray[1096]="Mercury";
manufactuersArray[1097]="Mercury";
manufactuersArray[1098]="Mercury";
manufactuersArray[1099]="Mercury";
manufactuersArray[1100]="Mercury";
manufactuersArray[1101]="Ford";
manufactuersArray[1102]="Ford";
manufactuersArray[1103]="Ford";
manufactuersArray[1104]="Ford";

uManufacturers = new Array();  // array to hold unique manufacturers
newMA = manufactuersArray.compact(); // create a new zero indexed version of  manufactuersArray
newMA.each( function(manufacturer){ // for each value in newMA
    if(uManufacturers.indexOf(manufacturer) < 0){ // see if that value is in the uManufacturers array
        uManufacturers.push(manufacturer);  // if not then add it to uManfacturers
   }
});

Use uManufacturers to populate your new select box.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now