459 questions
177
votes
7
answers
43k
views
Is Chrome’s JavaScript console lazy about evaluating objects?
I’ll start with the code:
var s = ["hi"];
console.log(s);
s[0] = "bye";
console.log(s);
Simple, right? In response to this, the Firefox console says:
[ "hi" ]
[ "...
1970
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 ...
3826
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 ...
5009
votes
64
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++...
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 ...
3377
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 ...
581
votes
3
answers
510k
views
How to return value from an asynchronous callback function? [duplicate]
This question is asked many times in SO. But still I can't get stuff.
I want to get some value from callback. Look at the script below for clarification.
function foo(address){
// google map ...
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";
...
1012
votes
33
answers
308k
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 ...
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?
3360
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 ...
680
votes
19
answers
885k
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 ...
556
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 ...
2148
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.
2332
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?