How to assign value to a property under an initialized parent property in javascript?

kenshinji

Say I have an object as below,

var ob = {}

Now I want to assign value to it as following

ob = {
  data: {
    id: '123456'
  }
}

I tried ob.data.id = '123456', it prompts me that

TypeError: Cannot set property 'id' of undefined

Unless I make it this way

ob.data = {}
ob.data.id = '123456'

I'm wondering if there a easier/elegant way to achieve the same goal here? Much appreciated.

Vivek
var ob = {};
ob.data.id = '123456';

The above code gives an error because, you have initialized the variable ob as an object but in the next line, you are trying to add a property id to ob.data. Here ob.data is not yet defined (undefined).

var ob = {};
ob.data = {};
ob.data.id = '123456';

This code works because, first you have initialized the variable ob as an object and then you have added a property data to it which is also an object and then finally adding the id property to the ob.data object.

As pointed out in the comment by @Lewis, you can combine the last two statements to a single statement as shown below.

var ob = {};
ob.data = {id: '123456'};

This would give the same result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to assign bean's property an Enum value in Spring config file?

How to dynamically assign value to class property in TypeScript

How to assign ItemsSource property

How to dynamically assign value to same property of an object?

mvc - How to assign a value to a partial view property from withing its parent view

Dynamically assign value to a Property

Assign value to list property of a class

How to Assign Async Value to Razor Component [Parameter] Property?

How to assign a value to optional property of class

Assign value to a class property in TypeScript

How to set a property initialized by @PropertySource

Accessing parent property in javascript

Assign a value to a property conditionally

How to assign value based on property in AngularJs

How do I assign a value to a WebViewPage property from a filter or a controller?

How to refer a property in a value of another property in the same Javascript Object?

How to assign value to class property of an element in Angular2

how to assign value class within class property

assign javascript array property declaratively

How to assign a reference value to a generic property

How get a date (value) from a DatePicker and assign it to property an object?

How to assign an entered value on a TextField to a String property?

Attempt to assign property "parent_id" on null

How to assign a property in object to the value of some property in same object in javascript?

From Nested object gather children's value and assign to parent Property

how to assign value to a get-only property

How to assign a value to a mutable reference of an object property?

How to assign a value to a specific object property in Array?

How to assign an object property value as a key in the same object?