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

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 0.54 KB  |  hits: 12  |  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. Get names and values from array in Javascript
  2. t = new Object();
  3. t.erg = new Array();
  4. t.erg['random1'] = "something1";
  5. t.erg['random2'] = "something2";
  6. t.erg['random3'] = "something3";
  7. t.erg['random4'] = "something4";
  8.        
  9. for (x in t.erg) {
  10.   if (t.erg.hasOwnProperty(x)) {
  11.     alert("key is " + x + " value is " + t.erg[x]);
  12.   }
  13. }
  14.        
  15. var randomVal = "random" + Math.floor(Math.random()*4);
  16.        
  17. t={
  18.   erg:{
  19.     'random1':'something1',
  20.     'random2':'something2'
  21.   }
  22. }
  23.  
  24. for(var x in t.erg){
  25.   //in the first round of the loop
  26.   //x holds random1
  27. }