Questions tagged [object]

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.

Objects in object-oriented programming (OOP) are data structures combined with the associated processing routines. Classes are sets of objects, while objects are instances of sets. Objects have members and methods, defining their properties and their abilities. Classes can have their own members and methods, which define the properties and abilities of the set of objects. For instance, if we have a Bird class, its objects might have an age property and a fly ability, while the Bird class might have a number of birds or a migrate ability, which is applicable for the set. Class-level methods are called static, or shared. For instance, a file could be represented as an object: a collection of data and the associated read and write routines. In typical object-oriented languages, all objects are instances of classes.

Properties of an object

Three properties characterize objects:

  • Identity: the property of an object that distinguishes it from other objects
  • State: describes the data stored in the object
  • Behavior: describes the methods in the object's interface by which the object can be used

See also:

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

Resource

64898 questions
1119
votes
31 answers

How can I access and process nested objects, arrays, or JSON?

I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)? For example: var data = { code: 42, items: [{ id: 1, name: 'foo' }, { …
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1018
votes
16 answers

How do I determine the size of an object in Python?

How do I get the size occupied in memory by an object in Python?
user46646
  • 153,461
  • 44
  • 78
  • 84
991
votes
45 answers

Sorting object property by values

If I have a JavaScript object such as: var list = { "you": 100, "me": 75, "foo": 116, "bar": 15 }; Is there a way to sort the properties based on value? So that I end up with list = { "bar": 15, "me": 75, "you": 100, "foo":…
Steerpike
  • 17,163
  • 8
  • 39
  • 53
965
votes
82 answers

How to determine equality for two JavaScript objects?

A strict equality operator will tell you if two object types are equal. However, is there a way to tell if two objects are equal, much like the hash code value in Java? Stack Overflow question Is there any kind of hashCode function in JavaScript? is…
user4903
960
votes
19 answers

Accessing an object property with a dynamically-computed name

I'm trying to access a property of an object using a dynamic name. Is this possible? const something = { bar: "Foobar!" }; const foo = 'bar'; something.foo; // The idea is to access something.bar, getting "Foobar!"
RichW
  • 10,692
  • 6
  • 26
  • 33
934
votes
14 answers

Does JavaScript guarantee object property order?

If I create an object like this: var obj = {}; obj.prop1 = "Foo"; obj.prop2 = "Bar"; Will the resulting object always look like this? { prop1 : "Foo", prop2 : "Bar" } That is, will the properties be in the same order that I added them?
mellowsoon
  • 22,273
  • 19
  • 57
  • 75
931
votes
25 answers

Why is null an object and what's the difference between null and undefined?

Why is null considered an object in JavaScript? Is checking if ( object == null ) Do something the same as if ( !object ) Do something ? And also: What is the difference between null and undefined?
rahul
  • 184,426
  • 49
  • 232
  • 263
914
votes
23 answers

How do I copy an object in Java?

Consider the code below: DummyBean dum = new DummyBean(); dum.setDummy("foo"); System.out.println(dum.getDummy()); // prints 'foo' DummyBean dumtwo = dum; System.out.println(dumtwo.getDummy()); // prints…
Veera
  • 32,532
  • 36
  • 98
  • 137
904
votes
61 answers

Most efficient method to groupby on an array of objects

What is the most efficient way to groupby objects in an array? For example, given this array of objects: [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, …
D'Arcy Rail-Ip
  • 11,505
  • 11
  • 42
  • 67
894
votes
23 answers

How to access the first property of a Javascript object?

Is there an elegant way to access the first property of an object... where you don't know the name of your properties without using a loop like for .. in or jQuery's $.each For example, I need to access foo1 object without knowing the name of…
atogle
  • 11,293
  • 4
  • 21
  • 13
889
votes
35 answers

How to pass an object from one activity to another on Android

I am trying to work on sending an object of my customer class from one Activity and displaying it in another Activity. The code for the customer class: public class Customer { private String firstName, lastName, address; int age; …
Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
882
votes
77 answers

How to remove all duplicates from an array of objects?

I have an object that contains an array of objects. obj = {}; obj.arr = new Array(); obj.arr.push({place:"here",name:"stuff"}); obj.arr.push({place:"there",name:"morestuff"}); obj.arr.push({place:"there",name:"morestuff"}); I'm wondering what is…
Travis
  • 10,192
  • 4
  • 19
  • 19
867
votes
64 answers

Test for existence of nested JavaScript object key

If I have a reference to an object: var test = {}; that will potentially (but not immediately) have nested objects, something like: {level1: {level2: {level3: "level3"}}}; What is the best way to check for the existence of property in deeply…
user113716
  • 318,772
  • 63
  • 451
  • 440
824
votes
11 answers

How to check if object property exists with a variable holding the property name?

I am checking for the existence of an object property with a variable holding the property name in question. var myObj; myObj.prop = "exists"; var myProp = "p"+"r"+"o"+"p"; if(myObj.myProp){ alert("yes, i have that property"); }; This is…
Slopeside Creative
  • 8,901
  • 4
  • 17
  • 18
815
votes
46 answers

How to convert an array into an object?

What is the best way to convert: ['a','b','c'] to: { 0: 'a', 1: 'b', 2: 'c' }
David Hellsing
  • 106,495
  • 44
  • 176
  • 212