使用变量访问对象的属性 [英] Accessing property of object with variable

查看:78
本文介绍了使用变量访问对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个对象,有name,phone_number等属性...我如何使用变量访问Javascript中的那些?

If I have an object, with properties like name, phone_number, etc...how can I access those in Javascript with a variable?

我想访问通过这样的方式在javascript中的属性名称:

I want to access the property name in javascript by doing something like this:

object {name : "bob", phone_number : "911" }
propertiesArray = ["name","phone_number"];

 object.propertiesArray[0]; // instead of  object.name;


推荐答案

object[propertiesArray[0]]

这样就可以了。使用 []

这篇关于使用变量访问对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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