Questions tagged [javascript-objects]

Use this tag for questions related to JavaScript objects.

In general an object is any entity that can be manipulated by commands in a programming language. An object can be a value, a variable, a function, or a complex data-structure. In object-oriented programming, an object refers to an instance of a class.

In JavaScript, almost "everything" is an object.

  • Booleans can be objects (or primitive data treated as objects)
  • Numbers can be objects (or primitive data treated as objects)
  • Strings can be objects (or primitive data treated as objects)
  • Dates are always objects
  • Maths are always objects
  • Regular expressions are always objects
  • Arrays are always objects
  • Functions are always objects
  • Objects are objects

This tag is specific to language so JavaScript-objects.

See also:

  • (Used as template for creating new objects)
  • (Object-oriented programming)

Attribution: https://stackoverflow.com/tags/object/info

Links

7142 questions
4850
votes
62 answers

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++) { if (a[i] === obj) { return…
brad
  • 73,826
  • 21
  • 73
  • 85
3893
votes
42 answers

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?
falmp
  • 39,347
  • 3
  • 21
  • 18
3701
votes
81 answers

How do I correctly clone a JavaScript object?

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 properties. This isn't a problem, since I'm copying…
soundly_typed
  • 39,257
  • 5
  • 28
  • 36
3310
votes
51 answers

How can I check if an object is an array?

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 fear of an error. So how do I check if the…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
3232
votes
69 answers

How can I merge properties of two JavaScript objects dynamically?

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 now has three properties: food, car, and…
JC Grubbs
  • 39,191
  • 28
  • 66
  • 75
2995
votes
44 answers

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"; myObject["lastname"] = "Simpson"; myObject["age"] = 21;
Gareth Simpson
  • 36,943
  • 12
  • 47
  • 50
2163
votes
55 answers

Check if a value is an object in JavaScript

How do you check if a value is an object in JavaScript?
Danny Fox
  • 38,659
  • 28
  • 68
  • 94
2099
votes
36 answers

Find object by id in an array of JavaScript objects

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 object in the array. How do I do this in…
thugsb
  • 22,856
  • 6
  • 30
  • 44
2044
votes
40 answers

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.
maxjackie
  • 22,386
  • 6
  • 29
  • 37
1848
votes
25 answers

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 containing the values, for example field foo would…
hyde
  • 60,639
  • 21
  • 115
  • 176
990
votes
34 answers

__proto__ VS. prototype in JavaScript

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 again to the Object.prototype. Thus, repeat,…
0x90
  • 39,472
  • 36
  • 165
  • 245
625
votes
19 answers

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 in parts (not all object properties at…
nkuhta
  • 10,388
  • 12
  • 41
  • 54
572
votes
3 answers

How to return value from an asynchronous callback function?

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 stuff geocoder.geocode( { 'address':…
Gowri
  • 16,587
  • 26
  • 100
  • 160
549
votes
25 answers

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 loop?
Mellon
  • 37,586
  • 78
  • 186
  • 264
486
votes
25 answers

How to do a deep comparison between 2 objects with lodash?

I have 2 nested objects which are different and I need to know if they have a difference in one of their nested properties. var a = {}; var b = {}; a.prop1 = 2; a.prop2 = { prop3: 2 }; b.prop1 = 2; b.prop2 = { prop3: 3 }; The object could be much…
JLavoie
  • 16,678
  • 8
  • 33
  • 39
1
2 3
99 100