7,176 questions
-7 votes
1 answer
173 views
How should I efficiently add all pairs in a key-value array to an object?
I have an object obj; and I then obtain an array arr of key-value pairs. Assuming that the key sets of obj and of arr are disjoint (i.e. no key appears in both) - how do I efficiently add all pairs in ...
5004 votes
63 answers
3.7m views
How do I check if an array includes a value in JavaScript?
What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++...
3823 votes
83 answers
2.5m views
How do I correctly clone a JavaScript object? [duplicate]
I have an object x. I'd like to copy it as object y, such that changes to y do not modify x. I realized that copying objects derived from built-in JavaScript objects will result in extra, unwanted ...
24 votes
2 answers
2k views
Why do JavaScript Websocket objects not get destroyed when they go out of scope?
In JavaScript, when you do something like this, function connect() { var ws = new WebSocket(url); ws.onmessage = function(e) { window.alert(e.data); }; } it will work. But why ...
3374 votes
69 answers
2.1m views
How can I merge properties of two JavaScript objects?
I need to be able to merge two (very simple) JavaScript objects at runtime. For example I'd like to: var obj1 = { food: 'pizza', car: 'ford' } var obj2 = { animal: 'dog' } obj1.merge(obj2); //obj1 ...
2328 votes
59 answers
2.5m views
Check if a value is an object in JavaScript
How do you check if a value is an object in JavaScript?
3889 votes
41 answers
4.4m views
How do I test for an empty JavaScript object?
After an AJAX request, sometimes my application may return an empty object, like: var a = {}; How can I check whether that's the case?
3358 votes
51 answers
2.1m views
How can I check if an object is an array? [duplicate]
I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item so I can loop over it without ...
3079 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"; ...
2160 votes
37 answers
2.5m views
Search an array of JavaScript objects for an object with a matching value
I've got an array: myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.] I'm unable to change the structure of the array. I'm being passed an id of 45, and I want to get 'bar' for that ...
1968 votes
26 answers
2.0m views
From an array of objects, extract value of a property as array
I have JavaScript object array with the following structure: objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ]; I want to extract a field from each object, and get an array ...
2146 votes
40 answers
2.6m views
How can I display a JavaScript object?
How do I display the content of a JavaScript object in a string format like when we alert a variable? The same formatted way I want to display an object.
1013 votes
33 answers
307k views
What is the difference between `.prototype` and `.__proto__`?
This figure again shows that every object has a prototype. Constructor function Foo also has its own __proto__ which is Function.prototype, and which in turn also references via its __proto__ property ...
680 votes
19 answers
881k views
How to iterate over a JavaScript object?
I have an object in JavaScript: { abc: '...', bca: '...', zzz: '...', xxx: '...', ccc: '...', // ... } I want to use a for loop to get its properties. And I want to iterate it ...
557 votes
25 answers
848k views
How to get all properties values of a JavaScript Object (without knowing the keys)?
If there is a JavaScript object: var objects={...}; Suppose, it has more than 50 properties, without knowing the property names (that's without knowing the 'keys') how to get each property value in a ...