Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.99 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. javascript - best way to convert values of object or array
  2. var legend = {
  3.     "html": "html5",
  4.     "css2": "css3",
  5.     "javascript": "jquery"  
  6. }
  7.  
  8. var old_array = new Array("html", "css2", "javascript");
  9. var new_array = [];
  10.  
  11. max = old_array.length;
  12.  
  13. // loop through values in original array
  14. for(i=0;i<max;i++){
  15.     for(var val in legend){
  16.             // looping through values in legend.  If match, convert
  17.         if(old_array[i] === val){
  18.             new_array[i] = legend[val];
  19.         }
  20.     }
  21. }
  22. console.log(new_array);
  23. document.write(new_array);
  24.        
  25. var legend = {
  26.     "html": "html5",
  27.     "css2": "css3",
  28.     "javascript": "jquery"  
  29. }
  30.  
  31. var old_array = new Array("html", "css2", "javascript");
  32. var new_array = [];
  33.  
  34. max = old_array.length;
  35.  
  36. // loop through values in original array
  37. for(var i=0;i<max;i++){
  38.     // see if array value is a key in the legend object
  39.     if ([old_array[i] in legend) {
  40.         new_array.push(legend[old_array[i]]);
  41.     }
  42. }
  43. console.log(new_array);
  44. document.write(new_array);