code_junkie

Javascript array with a mix of literals and arrays

Nov 10th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. area[0].states[0]
  2. area[0].cities[0]
  3.  
  4. var area = [
  5. {
  6. "State" : "Texas",
  7. "Cities" : ['Austin','Dallas','San Antonio']
  8. },
  9. {
  10. "State" :"Arkansas",
  11. "Cities" : ['Little Rock','Texarkana','Hot Springs']
  12. }
  13. ] ;
  14.  
  15. area = {
  16. "Texas": ['Austin','Dallas','San Antonio']
  17. }
  18.  
  19. states = {}
  20. for(var j=0; j<area.length; j++)
  21. states[ area[0].State ] = area[0].Cities
  22.  
  23. states["Texas"]
  24.  
  25. ['Austin','Dallas','San Antonio']
  26.  
  27. var area =
  28. {
  29. "Texas" : { "Cities" : ['Austin','Dallas','San Antonio'] },
  30. "Arkansas" : { "Cities" : ['Little Rock','Texarkana','Hot Springs'] }
  31. };
  32.  
  33. area["Texas"].Cities[0];
  34.  
  35. <select id="states" size="2"></select>
  36. <select id="cities" size="3"></select>
  37.  
  38. var area = [
  39. {
  40. "states" : "Texas",
  41. "cities" : ['Austin','Dallas','San Antonio']
  42. },
  43. {
  44. "states" :"Arkansas",
  45. "cities" : ['Little Rock','Texarkana','Hot Springs']
  46. }
  47. ] ;
  48.  
  49. $(function() { // create an array to be referenced by state name
  50. state = [] ;
  51. for(var i=0; i<area.length; i++) {
  52. state[area[i].states] = area[i].cities ;
  53. }
  54. });
  55.  
  56. $(function() {
  57. // populate states select box
  58. var options = '' ;
  59. for (var i = 0; i < area.length; i++) {
  60. options += '<option value="' + area[i].states + '">' + area[i].states + '</option>';
  61. }
  62. $("#states").html(options); // populate select box with array
  63.  
  64. // selecting state (change) will populate cities select box
  65. $("#states").bind("change",
  66. function() {
  67. $("#cities").children().remove() ; // clear select box
  68. var options = '' ;
  69. for (var i = 0; i < state[this.value].length; i++) {
  70. options += '<option value="' + state[this.value][i] + '">' + state[this.value][i] + '</option>';
  71. }
  72. $("#cities").html(options); // populate select box with array
  73. } // bind function end
  74. ); // bind end
  75. });
  76.  
  77. var area = {
  78. "Texas" : ["Austin","Dallas","San Antonio"],
  79. "Arkansas" : ["Little Rock","Texarkana","Hot Springs"]
  80. };
  81.  
  82. // area["Texas"] would return ["Austin","Dallas","San Antonio"]
Add Comment
Please, Sign In to add comment