【问题标题】:calculate the count of object using reduce or filter in javascript在 javascript 中使用 reduce 或 filter 计算对象的计数
【发布时间】:2019-04-04 15:25:33
【问题描述】:

我编写了一个方法来计算数组中“启用”设置为“真”的对象的数量。

每当它在我的数组中找到一个“启用”设置为“真”的对象时,我都会在计数器上加 1。

如果不使用 'counter' 变量并使用 reduce 或 filter 来代替,我怎么能做到这一点??

这是我的代码:

function getCount() {               
    const arr =[[{ "enabled": true }], [{ "enabled": false}, {"enabled": true}]];                       
    var count = 0;
    arr.forEach(function(ar){
        ar.forEach(function(obj){
            if(obj.enabled) {
                count++;
            }
        })
    });
    return count;           
}

【问题讨论】:

  • 什么是obj.seatMapAvailable?你的意思是obj.enabled
  • 是的,没错,现在更正了!我想使用 ES6 中的 reduce。

标签: javascript multidimensional-array filter ecmascript-6 reduce


【解决方案1】:

在我看来,使用 helper reduce 函数是最简单的实现:

const arr =[
 [
     {"enabled": true},
     {"enabled": true}
 ],
 [
     {"enabled": false}, 
     {"enabled": true},
     {"enabled": true}
 ]
]; 

// Helper function to count inner arrays
const r = (i) => i.reduce( (p, c) => c.enabled ? p = p + 1 : p, 0)

const count = arr.reduce( (p, c) => p + r(c), 0) // Output: 4

【讨论】:

    【解决方案2】:

    看看下面,我加了一条评论:

    [].concat(...arr) /* flatten the array */
    .filter(item => item.enabled) /* return only enabled: true */
    .length /* get the count */
    

    const arr = [
      [{
        "enabled": true
      }],
      [{
        "enabled": false
      }, {
        "enabled": true
      }]
    ];
    var enabledCount = [].concat(...arr).filter(item => item.enabled).length
    console.log(enabledCount)

    或者你可以使用reduce,如果你愿意的话

    const arr = [
      [{
        "enabled": true
      }],
      [{
        "enabled": false
      }, {
        "enabled": true
      }]
    ];
    
    var enabledCount = arr.reduce(
      (accumulator, currentValue) => accumulator.concat(currentValue), []
    ).filter(item => item.enabled).length
    
    console.log(enabledCount)

    【讨论】:

    • 我想使用 ES6 中的 reduce
    • 我添加了一个 reduce 示例
    【解决方案3】:

    你可以这样做:

    const arr = [[{ "enabled": true }], [{ "enabled": false }, { "enabled": true }]];
    
    console.log(
        arr.reduce(
        // Flatten the array of arrays
            (acc, curVal) => acc.concat(curVal), []         
        ).filter(
        // Filter so you have an array with 
        //       objects that have 'enable': true
            (obj) => Object.is(obj['enabled'], true))
        // and then return the length of it
        .length
    );
    

    【讨论】:

      【解决方案4】:

      这样的东西有用吗?

      const arr =[[{ "enabled": true }], [{ "enabled": false}, {"enabled": true}]];               
      const enabledArray = arr.map(function(item) {
          return item.filter(function(subItem){
              return subItem.enabled === true;
          })
      })
      const enabledItems = enabledArray.length;
      

      【讨论】:

        猜你喜欢
        • 2013-01-22
        • 2019-10-28
        • 2015-11-22
        • 2019-01-07
        • 1970-01-01
        • 2012-02-22
        • 2021-09-06
        • 2018-08-24
        • 2011-10-06
        相关资源
        最近更新 更多