【发布时间】:2019-01-16 05:38:45
【问题描述】:
假设我们有这样的对象:
foo = {
"currency": "dollar"
}
是否可以检查对象的值是否存在并且等于美元?
例如:
const bar = currency;
那么我们如何将 bar 的值传递给 foo 路径呢?或者使用任何应该用作 $bar 的语法?我不想将 $bar 作为路径名传递,而是将 bar 的值作为路径传递。
foo.$bar? true: false
in result foo.currency === true or false
甚至可以有另一个对象,例如:
anotherobject.anotherpath.$(getKey(foo))
要像 anotherobject.anotherpath.currency 这样的对象?
希望它足够清楚,可以理解
【问题讨论】:
-
您是否正在寻找
foo[bar]?喜欢foo[bar] == "dollar"? -
@Aioros,伙计,我不想检查值,但如果存在特定路径。这样我想确定路径 foo.$bar 是否存在,其中 $bar = currency.
-
你可以
if (bar in foo && foo[bar] === 'dollar')wherebar = 'currency'developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript typescript