16 Apr 2013

Mapping a key to many values in javascript

The concept of mapping arises when requires to bind a pair of values together like name:'john'. here name is a key and 'john' is value of key. Generally for mapping unique keys are used to identify particular value.
What if one wants to identify many values with same key ?

Real world example
One of my recent software project , it is required to access the weather feed for particular city. We are using yahoo developer api for that. First you have to get the id of a city , woied, you can can get this id for your city  by searching the http://weather.yahoo.com/.
Now use  this link http://weather.yahooapis.com/forecastrss?w=woeid   to get the data for the particular city. where woeid is the id of the city.

Problem
Lets get back to the mapping question, To describe the weather condition they have specified the description.of condition ( http://developer.yahoo.com/weather/#codes).
For example, All following four weather condition can be characterized as a 'rain'
              mixed rain and sleet
              mixed rain and snow
              drizzle rain
              heavy rain
So whenever either of four conditions are there , application requires to display as a 'rain'.
It was required to map 'rain '(key) to above four conditions (values)

Wrong solution
i created a dictionary having 'rain' as a key and a weather conditions.This wayi created four 'rain' key and map each of weather condition.


var dict = {     mixed rain and sleet,    
                       mixed rain and snow,   
                       drizzle rain,                  
                       heavy rain                    
                 }                                        

When i iterated over dict it show only last mapping.
                for(item in dict){                                   
                      Ti.API.info(item+"....."+dict[item])
                   }                                                          

Wrong Output:    
          rain......mixed rain and sleet.
But it supposed to show all four conditions, what happened? I realized that the dictionary key must be unique.what i was doing wrong? I was mapping multiple keys having same name which is being treated as a one single key.  So i have to map all four weather conditions against only one 'rain' key.



Right Solution
Created a dictionary, this time, with only one key and assigning all four weather conditions as an  array.
                 var dict = {                                                    
                                    'rain' : [   mixed rain and sleet  , 
                                                   mixed rain and snow,  
                                                   drizzle rain,                  
                                                   heavy rain                    
                                           ]                                            
                     }                                                                 
Iterating over dict
            for(item in dict){                                                                  
                        array_values = dict[item]                                         
                         for(ele in array_values){                                         
                               Ti.API.info(item+"......"+array_values[ele])     
                         }                                                                               
            }                                                                                            

Although above code is self explanatory, i'm going to explain anyhow. 'item' is a key which is 'rain'  and for dict['rain'] you get the array of values. Now you can  simply iterate over array, where 'ele' will give weather conditions.
  
Right Output:
         rain..... mixed rain and sleet,
         rain..... mixed rain and sleet,
         rain..... drizzle rain,
         rain..... heavy rain 

Now i can correctly use 'rain' in place of those four weather conditions.





No comments:

Post a Comment