Skip to main content
1139 votes
31 answers
1.3m views

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, ...
Felix Kling's user avatar
991 votes
22 answers
680k views

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, ...
RichW's user avatar
  • 11k
5157 votes
67 answers
3.0m 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(...
328 votes
14 answers
98k views

How can I sort arrays and data in PHP?

This question is intended as a reference for questions about sorting arrays in PHP. It is easy to think that your particular case is unique and worthy of a new question, but most are actually minor ...
deceze's user avatar
  • 523k
423 votes
16 answers
618k views

How do I print my Java object without getting "SomeType@2f92e0f4"?

I have a class defined as follows: public class Person { private String name; // constructor and getter/setter omitted } I tried to print an instance of my class: System.out.println(myPerson); ...
Duncan Jones's user avatar
  • 69.5k
993 votes
16 answers
491k views

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 ...
mellowsoon's user avatar
  • 23.4k
90 votes
6 answers
153k views

How can I access an array/object?

I have the following array and when I do print_r(array_values($get_user));, I get: Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => [email protected] ...
Muhamad Yulianto's user avatar
949 votes
63 answers
1.3m views

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 ...
D'Arcy Rail-Ip's user avatar
1032 votes
76 answers
972k views

How can I 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 ...
user avatar
1508 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 ...
spankmaster79's user avatar
881 votes
64 answers
419k views

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 ...
user113716's user avatar
  • 323k
954 votes
78 answers
1.3m views

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:"...
Travis's user avatar
  • 10.9k
1040 votes
46 answers
1.6m views

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 = { "...
Steerpike's user avatar
  • 17.6k
2487 votes
19 answers
1.2m 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()
ricree's user avatar
  • 36.8k
343 votes
13 answers
393k views

Add a property to a JavaScript object using a variable as the name?

I'm pulling items out of the DOM with jQuery and want to set a property on an object using the id of the DOM element. Example const obj = {} jQuery(itemsFromDom).each(function() { const element =...
Todd R's user avatar
  • 18.5k

15 30 50 per page
1
2 3 4 5
323