-1

I have the following Javascript object:-

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

Now I want to fetch dynamically the Party property value something like below. How can I do this?

alert(attributes.entityData.{attributes.entityType});
ilim
  • 4,477
  • 7
  • 27
  • 46
D.k
  • 442
  • 4
  • 13

2 Answers2

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]);
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
Rupesh Agrawal
  • 645
  • 8
  • 22
0

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.

BlackBeard
  • 10,246
  • 7
  • 52
  • 62