Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested objects in javascript, best practices

I would like to know the correct way to create a nested object in javascript. I want a base object called "defaultsettings". It should have 2 properties (object type): ajaxsettings and uisettings. I know that i can write something like

var defaultsettings = new Object(); var ajaxsettings = new Object();  defaultsettings.ajaxsettings = ajaxsettings.. etc. 

But what i want to know is how to type it this way (that i suppose is a more correct way of doing it):

var defaultsettings = {      var ajaxsettings = { ... } }; 

I suppose you get the idea. Thanks!

like image 269
Johan Avatar asked Oct 29 '11 23:10

Johan


People also ask

Can we have nested object in JavaScript?

In application code, objects are often nested. An object can have another object as a property, which could have a property, an array of even more objects. Nested objects are objects that are inside another object. You can create nested objects within a nested object.

How do you make a nested object in JavaScript?

const obj = { code: "AA", sub: { code: "BB", sub: { code: "CC", sub: { code: "DD", sub: { code: "EE", sub: {} } } } } }; Notice that for each unique couple in the string we have a new sub object and the code property at any level represents a specific couple. We can solve this problem using a recursive approach.

Which among these is are the correct way of accessing values of nested objects?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.

How do I update nested objects?

To update nested properties in a state object in React: Pass a function to setState to get access to the current state object. Use the spread syntax (...) to create a shallow copy of the object and the nested properties. Override the properties you need to update.


1 Answers

If you know the settings in advance you can define it in a single statement:

var defaultsettings = {                         ajaxsettings : { "ak1" : "v1", "ak2" : "v2", etc. },                         uisettings : { "ui1" : "v1", "ui22" : "v2", etc }                       }; 

If you don't know the values in advance you can just define the top level object and then add properties:

var defaultsettings = { }; defaultsettings["ajaxsettings"] = {}; defaultsettings["ajaxsettings"]["somekey"] = "some value"; 

Or half-way between the two, define the top level with nested empty objects as properties and then add properties to those nested objects:

var defaultsettings = {                         ajaxsettings : {  },                         uisettings : {  }                       };  defaultsettings["ajaxsettings"]["somekey"] = "some value"; defaultsettings["uisettings"]["somekey"] = "some value"; 

You can nest as deep as you like using the above techniques, and anywhere that you have a string literal in the square brackets you can use a variable:

var keyname = "ajaxsettings"; var defaultsettings = {}; defaultsettings[keyname] = {}; defaultsettings[keyname]["some key"] = "some value"; 

Note that you can not use variables for key names in the { } literal syntax.

like image 141
nnnnnn Avatar answered Oct 01 '22 09:10

nnnnnn