如何从基于键的对象文字返回值? [英] How to return a value from an Object Literal based on a key?

查看:80
本文介绍了如何从基于键的对象文字返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组如下.我将如何检索特定键的值并将该值放入变量中?

I have an array as follows. How would I retrieve the value of a specific key and put that value in a variable?

var obj = {"one":"1","two":"3","three":"5","four":"1","five":"6"};

例如,如果我想获取三"的值,我该如何在javascript或jQuery中做到这一点?

So for instance if I want to get the value of "three" how would I do it in javascript or jQuery?

推荐答案

您可以通过方括号表示法,就像这样:

You can do this via dot or bracket notation, like this:

var myVariable = obj.three;
//or:
var myVariable = obj["three"];

在第二个示例中,"three"可能是另一个变量中的字符串,可能是 .另外,为清楚起见,您所拥有的只是一个对象,而不是数组:)

In the second example "three" could be a string in another variable, which is probably what you're after. Also, for clarity what you have is just an object, not an array :)

这篇关于如何从基于键的对象文字返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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