【发布时间】:2019-06-05 14:19:55
【问题描述】:
见下文:
我有一堆像这样的 const:
const a = false
const b = false
const c = true
const d = false
const e = true
const f = true
我最终希望有两个如下所示的数组:
trueArray = [ c, e, f ]
falseArray = [ a, b, d ]
我正在地图上的 React Native 中构建一个过滤器系统,并且我正在获取一个位置类型是真(即显示)还是假(即不显示)的值,现在我已经做了每种类型的单独功能,即:
bar() {
if (bar === true) {
return 'bar';
}
return 0;
}
我的过滤器看起来像这样:
filter={[
['!has', 'point_count'],
['in', 'type'],
[
'!in',
'type',
this.activity(),
this.bar(),
this.cafe(),
etc.,
]]}
【问题讨论】:
-
为什么不将它们保存在单个数组或对象中?
-
像这样声明太多变量对我来说没有任何意义。
-
你说你想要
trueArray = [ c, e, f ]——你的意思是你想要trueArray = [true, true, true ]? -
@MaheerAli 也一样。然后把它们放在两个数组中也是没有意义的。您将有一个数组,其中所有项目都是
true,但您不知道这些是哪些变量。计算您拥有的每个标志的数量是非常浪费的。 -
let numberOfTrues = [a, b, c, d, e, f].filter(b => b).length;错误的数量只是6 - numberOfTrues。不过,不确定您会如何处理这些信息……
标签: javascript arrays sorting