Skip to content Skip to sidebar Skip to footer

How To Dynamically Access Value Of A Property In Javascript Object?

I have the following Javascript object:- var attributes = { entityData: { Party: 12 }, entityType: 'Party' }; Now I want to fetch dynamically the Party property value so

Solution 1:

whenever you need to access dynamic property you have to use square bracket for accessing property . Syntax: object[propery]

var attributes = {
    entityData: {
    Party: 12
  },
  entityType: "Party"
};

alert(attributes.entityData[attributes.entityType]);
alert(attributes.entityData[attributes.entityType]);

Solution 2:

You want something like this perhaps:

var attributes = {
    entityData: {
    Party: 12
  },
  entityType: "Party"
};

console.log(attributes.entityData[attributes['entityType']]);

Use square braces instead of curly braces.

Post a Comment for "How To Dynamically Access Value Of A Property In Javascript Object?"