65,005 questions
7585
votes
40
answers
3.4m
views
How do I remove a property from a JavaScript object?
Given an object:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
How do I remove the property ...
5157
votes
67
answers
3.1m
views
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(...
4136
votes
33
answers
3.8m
views
Checking if a key exists in a JavaScript object?
How do I check if a particular key exists in a JavaScript object or array?
If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
3247
votes
49
answers
1.5m
views
Detecting an undefined object property
How do I check if an object property in JavaScript is undefined?
3082
votes
44
answers
3.1m
views
Length of a JavaScript object
I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object?
const myObject = new Object();
myObject["firstname"] = "Gareth";
...
2536
votes
16
answers
1.6m
views
How can I check if an object has an attribute?
How do I check if an object has some attribute? For example:
>>> a = SomeClass()
>>> a.property
Traceback (most recent call last):
File "<stdin>", line 1, in <...
2494
votes
19
answers
1.3m
views
Calling a function of a module by using its name (a string)
How do I call a function, using a string with the function's name? For example:
import foo
func_name = "bar"
call(foo, func_name) # calls foo.bar()
2398
votes
32
answers
1.7m
views
Iterate through object properties
var obj = {
name: "Simon",
age: "20",
clothing: {
style: "simple",
hipster: false
}
}
for(var propt in obj){
console.log(propt + ': ' + obj[propt]);
}
...
2029
votes
2
answers
2.3m
views
How do I check if an object has a key in JavaScript? [duplicate]
Which is the right thing to do?
if (myObj['key'] == undefined)
or
if (myObj['key'] == null)
or
if (myObj['key'])
1717
votes
6
answers
468k
views
Why do Python classes inherit object?
Why does the following class declaration inherit from object?
class MyClass(object):
...
1514
votes
10
answers
1.5m
views
Object comparison in JavaScript [duplicate]
What is the best way to compare objects in JavaScript?
Example:
var user1 = {name : "nerd", org: "dev"};
var user2 = {name : "nerd", org: "dev"};
var eq = user1 == user2;
alert(eq); // gives false
I ...
1513
votes
11
answers
2.6m
views
Null object in Python
How do I refer to the null object in Python?
1391
votes
23
answers
1.8m
views
Convert JS object to JSON string
If I defined an object in JS with:
var j={"name":"binchen"};
How can I convert the object to JSON? The output string should be:
'{"name":"binchen"}'
1327
votes
21
answers
916k
views
Difference between ( for... in ) and ( for... of ) statements?
I know what is a for... in loop (it iterates over the keys), but I have heard about for... of for the first time (it iterates over values).
I am confused about for... of loop.
var arr = [3, 5, 7];
arr....
1248
votes
42
answers
2.4m
views
Converting an object to a string
How can I convert a JavaScript object into a string?
Example:
var o = {a:1, b:2}
console.log(o)
console.log('Item: ' + o)
Output:
Object { a=1, b=2} // very nice readable output :)
Item: [object ...