code_junkie

Dynamically creating keys in javascript associative array

Nov 12th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. arr['key'] = val;
  2.  
  3. { name: 'whatever' }
  4.  
  5. var text = ' name = oscar '
  6. var dict = new Array();
  7. var keyValuePair = text.split(' = ');
  8. dict[ keyValuePair[0] ] = 'whatever';
  9. alert( dict ); // prints nothing.
  10.  
  11. var a = new Array();
  12. a['name'] = 'oscar';
  13. alert(a['name']);
  14.  
  15. var text = 'name = oscar'
  16. var dict = new Array()
  17. var keyValuePair = text.replace(/ /g,'').split('=');
  18. dict[ keyValuePair[0] ] = keyValuePair[1];
  19. alert( dict[keyValuePair[0]] );
  20.  
  21. // create an empty associative array (in JavaScript it is called ... Object)
  22. var dict = {}; // huh? {} is a shortcut for "new Object()"
  23.  
  24. // add a key named fred with value 42
  25. dict.fred = 42; // we can do that because "fred" is a constant
  26. // and conforms to id rules
  27.  
  28. // add a key named 2bob2 with value "twins!"
  29. dict["2bob2"] = "twins!"; // we use the subscript notation because
  30. // the key is arbitrary (not id)
  31.  
  32. // add an arbitrary dynamic key with a dynamic value
  33. var key = ..., // insanely complex calculations for the key
  34. val = ...; // insanely complex calculations for the value
  35. dict[key] = val;
  36.  
  37. // read value of "fred"
  38. val = dict.fred;
  39.  
  40. // read value of 2bob2
  41. val = dict["2bob2"];
  42.  
  43. // read value of our cool secret key
  44. val = dict[key];
  45.  
  46. // change the value of fred
  47. dict.fred = "astra";
  48. // the assignment creates and/or replaces key-value pairs
  49.  
  50. // change value of 2bob2
  51. dict["2bob2"] = [1, 2, 3]; // any legal value can be used
  52.  
  53. // change value of our secret key
  54. dict[key] = undefined;
  55. // contrary to popular beliefs assigning "undefined" does not remove the key
  56.  
  57. // go over all keys and values in our dictionary
  58. for(key in dict){
  59. // for-in loop goes over all properties including inherited properties
  60. // let's use only our own properties
  61. if(dict.hasOwnProperty(key){
  62. console.log("key = " + key + ", value = " + dict[key]);
  63. }
  64. }
  65.  
  66. // let's delete fred
  67. delete dict.fred;
  68. // fred is removed, the rest is still intact
  69.  
  70. // let's delete 2bob2
  71. delete dict["2bob2"];
  72.  
  73. // let's delete our secret key
  74. delete dict[key];
  75.  
  76. // now dict is empty
  77.  
  78. // let's replaced it recreating all original data
  79. dict = {
  80. fred: 42,
  81. "2bob2": "twins!"
  82. // we can't add the original secret key because it was dynamic,
  83. // we can only add static keys
  84. // ...
  85. // oh well
  86. temp1: val
  87. };
  88. // let's rename temp1 into our secret key:
  89. if(key != "temp1"){
  90. dict[key] = dict.temp1; // copy the value
  91. delete dict.temp1; // kill the old key
  92. }else{
  93. // do nothing, we are good ;-)
  94. }
  95.  
  96. var f = new Object(); f.name = 'orion';
  97. var f = new Object(); f['name'] = 'orion';
  98. var f = new Array(); f.name = 'orion';
  99. var f = new Array(); f['name'] = 'orion';
  100. var f = new XMLHttpRequest(); f['name'] = 'orion';
  101.  
  102. var text = '{ name = oscar }'
  103. var dict = new Object();
  104.  
  105. // Remove {} and spaces
  106. var cleaned = text.replace(/[{} ]/g, '');
  107.  
  108. // split into key and value
  109. var kvp = cleaned.split('=');
  110.  
  111. // put in the object
  112. dict[ kvp[0] ] = kvp[1];
  113. alert( dict.name ); // prints oscar.
  114.  
  115. var myArray = new Array();
  116. myArray['one'] = 1;
  117. myArray['two'] = 2;
  118. myArray['three'] = 3;
  119.  
  120. // show the values stored
  121. for (var i in myArray) {
  122. alert('key is: ' + i + ', value is: ' + myArray[i]);
  123. }
  124.  
  125. $(function () {
  126. var id;
  127. var QueueInfo = new Object();
  128.  
  129. id = 1;
  130. QueueInfo[id] = ["a", "b", "c"];
  131.  
  132. id = 3;
  133. if (QueueInfo[id] == null) {
  134. QueueInfo[id] = ["a", "b", "c"]; // if array is null initialise it to 3 items
  135. }
  136. else {
  137. QueueInfo[id].push("d"); // if it is not then push 4th item to it
  138. }
  139.  
  140. if (QueueInfo[id] == null) {
  141. QueueInfo[id] = ["a", "b", "c"];
  142. }
  143. else {
  144. QueueInfo[id].push("d");
  145. }
  146.  
  147. QueueInfo[id].splice(0, QueueInfo[id].length - 1); // remove all except last element
  148.  
  149. delete QueueInfo[id]; // delete element
  150.  
  151. alert(QueueInfo[3]); // get element
  152.  
  153. });
Add Comment
Please, Sign In to add comment