Reputation: 1793
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>
Upvotes: 0
Views: 49
Reputation: 2321
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" }
]
}
Upvotes: 1
Reputation: 6946
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.
Upvotes: 0
Reputation: 279
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]];
Upvotes: 1
Reputation: 204
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
Upvotes: 3