All In One Script
MENU

How do I remove a property from a JavaScript object?

by 12:36:00 AM
undefined
How do I remove a property from a JavaScript object? Say I create an object as follows: var myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; What is the best way to remove the property regex to end up with new myObject as follows? var myObject = { "ircEvent": "PRIVMSG", "method": "newURI" }; Answer: Like this: delete myObject.regex; // or, delete myObject['regex']; // or, var prop = "regex"; delete myObject[prop]; For anyone interested in reading more about it, Stack Overflow user kangax has written an incredibly in-depth blog post

How do I check if an array includes an object in JavaScript?

by 4:31:00 AM
undefined
How do I check if an array includes an object in JavaScript? What is the most concise and efficient way to find out if a JavaScript array contains an object? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) { return true; } } return false; } Is there a better and more concise way to accomplish this? This is very closely related to Stack

How to remove a property from a JavaScript object?

by 4:32:00 AM
undefined
How to remove a property from a JavaScript object? Say I create an object as follows: var myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; What is the best way to remove the property regex to end up with new myObject as follows? var myObject = { "ircEvent": "PRIVMSG", "method": "newURI" }; Answer:  Like this: delete myObject.regex; // or, delete myObject['regex']; // or, var prop = "regex"; delete myObject[prop]; For anyone interested in reading more about it, Stack Overflow user kangax has written an incredibly in-depth blog post about

What is the most efficient way to deep clone an object in JavaScript?

by 4:30:00 AM
undefined
What is the most efficient way to deep clone an object in JavaScript? What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's non-standard and only supported by Firefox. I've done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency. I've also seen recursive copying functions with various flaws. I'm surprised no canonical solution exists. Answer :        Note: This is a reply to another answer, not a proper response to this question. If you wish to

Length of a JavaScript object

by 4:25:00 AM
undefined
Length of a JavaScript object If I have a JavaScript object, say var myObject = new Object(); myObject["firstname"] = "Gareth"; myObject["lastname"] = "Simpson"; myObject["age"] = 21; is there a built-in or accepted best practice way to get the length of this object? Answer :  The most robust answer (i.e. that captures the intent of what you're trying to do while causing the fewest bugs) would be: Object.size = function(obj) { var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) size++;

Instagram