如何动态访问Javascript对象中的属性值? [英] How to dynamically access value of a property in Javascript object?

查看:50
本文介绍了如何动态访问Javascript对象中的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Javascript 对象:-

I have the following Javascript object:-

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

现在我想动态获取 Party 属性值,如下所示.我怎样才能做到这一点?

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

alert(attributes.entityData.{attributes.entityType});

推荐答案

你可能想要这样的:

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

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

使用方括号代替花括号.

Use square braces instead of curly braces.

这篇关于如何动态访问Javascript对象中的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆