Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
  2.  
  3. var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI"};
  4.  
  5. delete myJSONObject.regex;
  6. // or,
  7. delete myJSONObject['regex'];
  8. // or,
  9. var prop = "regex";
  10. delete myJSONObject[prop];
  11.  
  12. var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
  13.  
  14. delete myJSONObject.regex;
  15.  
  16. alert ( myJSONObject.regex);
  17.  
  18. var obj = {
  19. field: 1
  20. };
  21. delete obj.field;
  22.  
  23. var obj = {
  24. field: 1
  25. };
  26. obj.field = undefined;
  27.  
  28. console.log('* -> "Takes prototype inheritance into consideration, that means it lookups all over prototype chain too."');
  29.  
  30. console.log(obj.field === undefined, 'obj.field === undefined', 'You get "undefined" value when querying for "field" in object-hashmap. *');
  31.  
  32. console.log(obj["field"] === undefined, 'obj["field"] === undefined', 'Just another way to query (equivalent). *');
  33.  
  34. console.log(typeof obj.field === "undefined", 'typeof obj.field === "undefined"', 'Get the value attached to "field" key, and check it's type is "undefined". *');
  35.  
  36. console.log(! ("field" in obj), '! ("field" in obj)', 'This statement returns true if "field" key exists in the hashmap. False otherwise. *');
  37.  
  38. console.log(obj.hasOwnProperty("field"), 'obj.hasOwnProperty("field")', 'This statement returns true if 'field' key exists in the hashmap. The ONLY way NOT to lookup for property in the prototype chain!');
  39. //Object.keys().indexOf() is an overkill :)
  40.  
  41. var counter = 0,
  42. key;
  43. for (key in obj) {
  44. counter++;
  45. }
  46. console.assert(counter === 0, 'counter === 0', '"field" is not iterated using "for .. in" loop. *');
  47.  
  48. var array = [1,2,3,4];
  49. delete array[2];
  50. //Expected result --> [1,2,4]
  51. //Actual result --> [1,2,null,4]
  52.  
  53. Array.prototype.remove = function(index){
  54. delete this[index];
  55. return this;
  56. };
  57. Array.prototype.clean = function(){
  58. var arr1 = this, arr2 = [];
  59. for(var a in arr1){
  60. if(arr1[a]&&arr1.hasOwnProperty(a)){
  61. arr2.push(arr1[a]);
  62. }
  63. }
  64. this.splice(0);
  65. for(var b in arr2){
  66. if(arr2.hasOwnProperty(b)){
  67. this.push(arr2[b]);
  68. }
  69. }
  70. return this;
  71. };
  72. var array = [1,2,3,4];
  73. array.remove(2).clean();
  74. // Result --> [1,2,4]
  75.  
  76. Array.prototype.remove = function(index){
  77. this.splice(index,1);
  78. }
  79.  
  80. myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
  81.  
  82. console.log(Object.keys(myJSONObject));
  83.  
  84. ["ircEvent", "method", "regex"]
  85.  
  86. delete myJSONObject["regex"];
  87.  
  88. ["ircEvent", "method"]
  89.  
  90. myJSONObject["regex"] = null;
  91. delete myJSONObject["regex"];
  92.  
  93. var regex = myJSONObject["regex"];
  94.  
  95. var myOtherObject = {};
  96. myOtherObject["regex"] = myJSONObject["regex"];
  97.  
  98. delete regex; //False
  99.  
  100. myOtherObject["regex"] = null;
  101. delete myOtherObject["regex"];
  102.  
  103. var myJSONObject =
  104. {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
  105.  
  106. _.pick(myJSONObject, "ircEvent", "method");
  107. => {"ircEvent": "PRIVMSG", "method": "newURI"};
  108.  
  109. var myJSONObject =
  110. {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
  111.  
  112. _.omit(myJSONObject, "regex");
  113. => {"ircEvent": "PRIVMSG", "method": "newURI"};
  114.  
  115. var obj = {"property":"value", "property2":"value"};
  116.  
  117. if (obj && obj.hasOwnProperty("property2")) {
  118. delete obj.property2;
  119. } else {
  120. //error handling
  121. }
  122.  
  123. //Inconsistent behavior if the same object is stored in an
  124. //array or object and also somewhere else at the same time.
  125. //Any inconsistency.
  126.  
  127. function my_delete (objectOrArray, index) {
  128. if (objectOrArray.splice) {
  129. objectOrArray.splice(index,1);
  130. } else {
  131. delete(objectOrArray[index]);
  132. }
  133. }
  134.  
  135. var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
  136. delete myJSONObject.regex;
  137. alert ( myJSONObject.regex);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement