[英]Access data of associative array inside associative array
Is it possible to access data of an associative array inside an associative array? 是否可以访问关联数组内部的关联数组的数据?
For instance, 例如,
foo = {
subFooA: 2,
subFooB: subFooA
}
or 要么
foo = {
subFooA: 2
}
foo = {
subFooB: subFooA
}
-Edit- Thank you for the answers... so I assume it is not possible for this -编辑-谢谢您的回答...所以我认为这是不可能的
filter = {
leadlag: {
parameters: {
K: 1,
fz: 20,
fp: 40,
wz: 2*math.pi*20,
wp: 2*math.pi*40
}
}
}
filter.leadlag.continuous = {
a: [filter.leadlag.parameters.K*filter.leadlag.parameters.wp*filter.leadlag.parameters.wz, filter.leadlag.parameters.K*filter.leadlag.parameters.wp],
b: [filter.leadlag.parameters.wp*filter.leadlag.parameters.wz, filter.leadlag.parameters.wz]
}
to be done more easily? 做起来更容易?
Yes it is possible, you may follow different patterns as well: 是的,有可能,您也可以遵循不同的模式:
var foo = {}
foo.subFooB = foo.subFooA = 2;
I always prefer using setter functions though as a cleaner approach: 我总是更喜欢使用setter函数,尽管它是一种更干净的方法:
function setFoo() {
return 2;
}
var foo = {
subfooA: setFoo(),
subFooB: setFoo()
}
Answer to your question v 2.0: 回答您的问题v 2.0:
Yes it is possible and readable with a helper variable which holds the reference to the object. 是的,它可以并且可以通过一个持有该对象引用的辅助变量来读取。
var filter = { leadlag: { parameters: { K: 1, fz: 20, fp: 40, wz: 2 * Math.PI * 20, wp: 2 * Math.PI * 40 } } }, p = filter.leadlag.parameters; // helper variable for better accessing filter.leadlag.continuous = { a: [pK * p.wp * p.wz, pK * p.wp], b: [p.wp * p.wz, p.wz] } document.write('<pre>' + JSON.stringify(filter, 0, 4) + '</pre>');
The first is not possible, while with the second you were almost there: 第一个是不可能的,而第二个则几乎要存在:
var foo = {
subFooA: 2
};
foo.subFooB = foo.subFooA;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.