This question already has an answer here:
这个问题已经有了答案:
- Dynamically access object property using variable 10 answers
- 使用变量10回答动态访问对象属性
If I have a javascript object that looks like below
如果我有一个如下所示的javascript对象
var columns = {
left: true,
center : false,
right : false
}
and I have a function that is passed both the object, and a property name like so
我有一个传递对象和属性名的函数
//should return false
var side = read_prop(columns, 'right');
what would the body of read_prop(object, property)
look like?
read_prop(对象、属性)的主体是什么样子的?
3 个解决方案
#1
546
You don't need a function for it - simply use the bracket notation:
你不需要一个函数,只需使用括号符号:
var side = columns['right'];
This is equal to dot notation, var side = columns.right;
, except the fact that right
could also come from a variable, function return value, etc., when using bracket notation.
它等于点表示法,var边= column .right,除了使用括号表示法时,right也可以来自变量、函数返回值等。
If you NEED a function for it, here it is:
如果你需要一个函数,它是:
function read_prop(obj, prop) {
return obj[prop];
}
#2
61
ThiefMaster's answer is 100% correct, although I came across a similar problem where I needed to fetch a property from a nested object (object within an object), so as an alternative to his answer, you can create a recursive solution that will allow you to define a nomenclature to grab any property, regardless of depth:
ThiefMaster的回答是100%正确的,虽然我遇到类似的问题,我需要获取一个属性从一个嵌套对象(对象在一个对象),替代他的回答,您可以创建一个递归解决方案将允许您定义一个术语来抓住任何财产,无论深度:
function fetchFromObject(obj, prop) {
if(typeof obj === 'undefined') {
return false;
}
var _index = prop.indexOf('.')
if(_index > -1) {
return fetchFromObject(obj[prop.substring(0, _index)], prop.substr(_index + 1));
}
return obj[prop];
}
Where your string reference to a given property ressembles property1.property2
对于给定属性的字符串引用ressembles 1.property2
Code and comments in JsFiddle.
在JsFiddle的代码和注释。
#3
5
Since I was helped with my project by the answer above (I asked a duplicate question and was referred here), I am submitting an answer (my test code) for bracket notation when nesting within the var:
由于上面的答案帮助我完成了我的项目(我问了一个重复的问题并在这里被引用),所以当在var中嵌套时,我提交了一个括号表示法的答案(我的测试代码):
<html>
<head>
<script type="text/javascript">
function displayFile(whatOption, whatColor) {
var Test01 = {
rectangle: {
red: "RectangleRedFile",
blue: "RectangleBlueFile"
},
square: {
red: "SquareRedFile",
blue: "SquareBlueFile"
}
};
var filename = Test01[whatOption][whatColor];
alert(filename);
}
</script>
</head>
<body>
<p onclick="displayFile('rectangle', 'red')">[ Rec Red ]</p>
<br/>
<p onclick="displayFile('square', 'blue')">[ Sq Blue ]</p>
<br/>
<p onclick="displayFile('square', 'red')">[ Sq Red ]</p>
</body>
</html>
#1
546
You don't need a function for it - simply use the bracket notation:
你不需要一个函数,只需使用括号符号:
var side = columns['right'];
This is equal to dot notation, var side = columns.right;
, except the fact that right
could also come from a variable, function return value, etc., when using bracket notation.
它等于点表示法,var边= column .right,除了使用括号表示法时,right也可以来自变量、函数返回值等。
If you NEED a function for it, here it is:
如果你需要一个函数,它是:
function read_prop(obj, prop) {
return obj[prop];
}
#2
61
ThiefMaster's answer is 100% correct, although I came across a similar problem where I needed to fetch a property from a nested object (object within an object), so as an alternative to his answer, you can create a recursive solution that will allow you to define a nomenclature to grab any property, regardless of depth:
ThiefMaster的回答是100%正确的,虽然我遇到类似的问题,我需要获取一个属性从一个嵌套对象(对象在一个对象),替代他的回答,您可以创建一个递归解决方案将允许您定义一个术语来抓住任何财产,无论深度:
function fetchFromObject(obj, prop) {
if(typeof obj === 'undefined') {
return false;
}
var _index = prop.indexOf('.')
if(_index > -1) {
return fetchFromObject(obj[prop.substring(0, _index)], prop.substr(_index + 1));
}
return obj[prop];
}
Where your string reference to a given property ressembles property1.property2
对于给定属性的字符串引用ressembles 1.property2
Code and comments in JsFiddle.
在JsFiddle的代码和注释。
#3
5
Since I was helped with my project by the answer above (I asked a duplicate question and was referred here), I am submitting an answer (my test code) for bracket notation when nesting within the var:
由于上面的答案帮助我完成了我的项目(我问了一个重复的问题并在这里被引用),所以当在var中嵌套时,我提交了一个括号表示法的答案(我的测试代码):
<html>
<head>
<script type="text/javascript">
function displayFile(whatOption, whatColor) {
var Test01 = {
rectangle: {
red: "RectangleRedFile",
blue: "RectangleBlueFile"
},
square: {
red: "SquareRedFile",
blue: "SquareBlueFile"
}
};
var filename = Test01[whatOption][whatColor];
alert(filename);
}
</script>
</head>
<body>
<p onclick="displayFile('rectangle', 'red')">[ Rec Red ]</p>
<br/>
<p onclick="displayFile('square', 'blue')">[ Sq Blue ]</p>
<br/>
<p onclick="displayFile('square', 'red')">[ Sq Red ]</p>
</body>
</html>