Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
- var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI"};
- delete myJSONObject.regex;
- // or,
- delete myJSONObject['regex'];
- // or,
- var prop = "regex";
- delete myJSONObject[prop];
- var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
- delete myJSONObject.regex;
- alert ( myJSONObject.regex);
- var obj = {
- field: 1
- };
- delete obj.field;
- var obj = {
- field: 1
- };
- obj.field = undefined;
- console.log('* -> "Takes prototype inheritance into consideration, that means it lookups all over prototype chain too."');
- console.log(obj.field === undefined, 'obj.field === undefined', 'You get "undefined" value when querying for "field" in object-hashmap. *');
- console.log(obj["field"] === undefined, 'obj["field"] === undefined', 'Just another way to query (equivalent). *');
- console.log(typeof obj.field === "undefined", 'typeof obj.field === "undefined"', 'Get the value attached to "field" key, and check it's type is "undefined". *');
- console.log(! ("field" in obj), '! ("field" in obj)', 'This statement returns true if "field" key exists in the hashmap. False otherwise. *');
- 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!');
- //Object.keys().indexOf() is an overkill :)
- var counter = 0,
- key;
- for (key in obj) {
- counter++;
- }
- console.assert(counter === 0, 'counter === 0', '"field" is not iterated using "for .. in" loop. *');
- var array = [1,2,3,4];
- delete array[2];
- //Expected result --> [1,2,4]
- //Actual result --> [1,2,null,4]
- Array.prototype.remove = function(index){
- delete this[index];
- return this;
- };
- Array.prototype.clean = function(){
- var arr1 = this, arr2 = [];
- for(var a in arr1){
- if(arr1[a]&&arr1.hasOwnProperty(a)){
- arr2.push(arr1[a]);
- }
- }
- this.splice(0);
- for(var b in arr2){
- if(arr2.hasOwnProperty(b)){
- this.push(arr2[b]);
- }
- }
- return this;
- };
- var array = [1,2,3,4];
- array.remove(2).clean();
- // Result --> [1,2,4]
- Array.prototype.remove = function(index){
- this.splice(index,1);
- }
- myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
- console.log(Object.keys(myJSONObject));
- ["ircEvent", "method", "regex"]
- delete myJSONObject["regex"];
- ["ircEvent", "method"]
- myJSONObject["regex"] = null;
- delete myJSONObject["regex"];
- var regex = myJSONObject["regex"];
- var myOtherObject = {};
- myOtherObject["regex"] = myJSONObject["regex"];
- delete regex; //False
- myOtherObject["regex"] = null;
- delete myOtherObject["regex"];
- var myJSONObject =
- {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
- _.pick(myJSONObject, "ircEvent", "method");
- => {"ircEvent": "PRIVMSG", "method": "newURI"};
- var myJSONObject =
- {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
- _.omit(myJSONObject, "regex");
- => {"ircEvent": "PRIVMSG", "method": "newURI"};
- var obj = {"property":"value", "property2":"value"};
- if (obj && obj.hasOwnProperty("property2")) {
- delete obj.property2;
- } else {
- //error handling
- }
- //Inconsistent behavior if the same object is stored in an
- //array or object and also somewhere else at the same time.
- //Any inconsistency.
- function my_delete (objectOrArray, index) {
- if (objectOrArray.splice) {
- objectOrArray.splice(index,1);
- } else {
- delete(objectOrArray[index]);
- }
- }
- var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
- delete myJSONObject.regex;
- alert ( myJSONObject.regex);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement