0

I am trying to populate dropdown(s) on select of an option in 1st dropdown(product), using pure java script. But got struck at mapping respective files to product, especially when the same version number repeats for different products

following is the data set.

 productA => version1.0.0 => FileA1.zip
 productA => version1.0.1 => FileA2.zip

 productB => version3.5.0 => FileB1.zip
 productB => version4.0.1 => FileB2.zip

 productC => version1.0.0 => FileC1.zip
 productC => version1.0.1 => FileC2.zip

My javascript arrays

 var ProductNameMap = {
        "ProductA":["1.0.0","1.0.1"],
        "ProductB":["3.5.0","4.0.1"], 
        "ProductC":["1.0.0","1.0.1"],   
};

//want to map files specifically associated with particular product
var ProductSeriesMap = {
    "version1.0.0":["FileA1.zip"],
    "version1.0.1":["FileA2.zip"]
    };

How can I differentiate between ProductA => version1.0.0 and ProductC => version1.0.0 during this mapping ?

My html

 <html>
 <body>
   Product:
<select id="product" onchange="changeproduct">
    <option>--Choose Product--</option>
</select><br/>
Version:<select id="version"  onchange="changeversion" ></select><br/>
File:<select id="file"></select>
<script>
//have my java script here to populate dropdowns 
</script>

4 Answers 4

3

have it all in one structure

var ProductsMap = {
        "ProductA":{"1.0.0":"File1-a1.zip","1.0.1":"File-a2.zip"},
        "ProductC":{"1.0.0":"File-c1.zip","1.0.1":"File-c2.zip"},   
};

when user select ProductA you have all versions and associated files

ProductsMap.ProductA

when user select ProductC you have all versions and associated files at

ProductsMap.ProductC

Sign up to request clarification or add additional context in comments.

Comments

1
var ProductSeriesMap={
"ProductA": new Map{"version1.0.0":["FileA1.zip"],"version1.0.1":["FileA2.zip"]},
"ProductB": new Map{"version1.0.0":["FileA1.zip"],"version1.0.1":["FileA2.zip"]},
}

You need to create like this;

Map[key,Map[key,value]];

1 Comment

It should be pointed out that Maps require somewhat recent browser support: kangax.github.io/compat-table/es6/#Map
1

I think changing up your objects a little bit would help. You can iterate over this structure with a for in loop (or using Object.keys(products) if your target browsers support it). Just tweak to fit your exact needs.

var products = {
    "A": [
        { "1.0.0": "FileA1.zip" },
        { "1.0.1": "FileA2.zip" }
    ],
    "B": [
        { "3.5.0": "FileB1.zip" },
        { "4.0.1": "FileB2.zip" }
    ],
    "C": [
        { "1.0.0": "FileC1.zip" },
        { "1.0.1": "FileC2.zip" }
    ]
}

2 Comments

You have extra commas after last array item item, that's bad syntax.
Yep, Copy+Paste. Removed. Thanks.
0

Your arrays are actually objects. What I don't understand is why you don't map all things together in your javascript object from your dataset directly, cause indeed with your second "array" you loose all trace of products, which can't be retrieved as you don't have a standard structure.

Your Javascript object structure could look something like :

var ProductNameMap = {
        "ProductA":[{"version":"1.0.0","fileName":"FileA1.zip"},{"version":"1.0.1","fileName":"FileA2.zip"}],
        "ProductB":[{"version":"3.5.0","fileName":"FileB1.zip"},{"version":"4.0.1","fileName":"FileB2.zip"}], 
        "ProductC":[{"version":"1.0.0","fileName":"FileC1.zip"},{"version":"1.0.1","fileName":"FileC2.zip"}]   
};

Other structures are possible depending on your needs of course, but without the context it's hard to point one as the best one... Basically if you need collection to be ordered (like succession of versions) you will need to use an array and for a collection where the order doesn't matter, an object is fine.

2 Comments

Thanks for the answer. And I am sorry for not making the context clear. When the user selects productA in 1st dropbox, I want to populate the versions dynamically in 2nd dropbox. When he selects version, I want to populate respective files for that specific version of the product. Can you help in achieving that using this above mentioned array?
This data structure would definitely be the best for what you're trying to achieve, the one from benjamin solum is the same with syntax errors (extra commas after last array item) and the others proposed won't preserve order. From there you should already try to code something if you wish to get any help in here as I won't code this for you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.