【问题标题】:How do you sort a list of consts that are either true or false, into two separate arrays, one with the true objects, and one with the false objects?如何将一个真或假的 const 列表分类为两个单独的数组,一个包含真对象,一个包含假对象?
【发布时间】: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


【解决方案1】:

无法获取在作用域中声明的变量名列表。您需要为此使用对象。

您应该创建一个对象,其键为a,b...,值为truefalse。然后就可以filter()对象的键了。

const obj = {
  a:false,
  b:false,
  c:true,
  d:false,
  e:true,
  f:true
}

let truthy = Object.keys(obj).filter(x => obj[x]);
let falsy = Object.keys(obj).filter(x => !obj[x]);

console.log(truthy);
console.log(falsy)

【讨论】:

    【解决方案2】:

    这是另一个使用 rxjs 及其 partition 运算符的解决方案:

    const obj = {
      a: false,
      b: false,
      c: true,
      d: false,
      e: true,
      f: true
    }
    
    const source = rxjs.from(Object.keys(obj));
    let [trueArray, falseArray] = source.pipe(rxjs.operators.partition(key => obj[key]));
    
    (async () => {
      trueArray = await trueArray.pipe(rxjs.operators.combineAll()).toPromise();
      falseArray = await falseArray.pipe(rxjs.operators.combineAll()).toPromise();
      
      console.log('trueArray', trueArray);
      console.log('falseArray', falseArray);
    })();
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.2/rxjs.umd.min.js"></script>

    【讨论】:

      【解决方案3】:

      这似乎是一件无用的事情,因为所有布尔值都是相同的。相反,您可以循环计算truefalse 的数量。

      let trueCount = 0;
      let falseCount = 0;
      for (let val of myVals) {
          val ? trueCount++ : falseCount++;
      }
      

      【讨论】:

        【解决方案4】:

        好吧,如果您使用的是 ES6 及更高版本,则可以将所有 const 放入一个数组中,然后通过其真或假对象对其进行过滤。下面的代码实现了这个想法:

        const a = false;
        const b = false;
        const c = true;
        const d = false;
        const e = true;
        const f = true;
        
        let completeArray = [a, b, c, d, e, f];
        
        let trueArray = completeArray .filter(function(object){
            return object;
        });
        
        let falseArray = completeArray .filter(function(object){
            return !object;
        });
        

        【讨论】:

        • 我不确定为什么 ES6 与该问题相关。无论如何你都可以这样做。事实上,如果你不使用 ES6,那么你甚至不会有 const 关键字,但你仍然可以使用 var 做同样的事情。
        • 当您使用 ES6 时,请使用 o => o/o => !o 而不是笨拙的 function 构造。
        猜你喜欢
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-28
        • 1970-01-01
        • 1970-01-01
        • 2011-10-28
        相关资源
        最近更新 更多