【问题标题】:JS reduce - sum only array lengths in objectJS reduce - 仅对对象中的数组长度求和
【发布时间】:2019-11-10 06:43:50
【问题描述】:

我有以下数据结构:

const arr = [    
    {key: 0, x: [], y: []},
    {key: 0, x: [], y: []}
]

我想检查所有数组是否为空,我有如何使用reduce来做到这一点?到目前为止我的代码:

arr.reduce((count, acc) => {
    return acc !== 'key' ? count + acc.length : count
}, 0)

【问题讨论】:

  • 您想查看哪个属性?
  • 您要检查xy 数组的长度吗?
  • 我要对所有数组的长度求和
  • 真的只是xy还是有更多的属性是数组?
  • 你想要什么长度或知道是否全部为零?

标签: javascript arrays object ecmascript-6 reduce


【解决方案1】:

您可以将key 从对象中取出并检查所有长度是否为零。

const
    array = [{ key: 0, x: [], y: [] }, { key: 0, x: [], y: [] }],
    allZero = array.every(({ key, ...o }) =>
        Object.values(o).every(({ length }) => !length));

console.log(allZero);

求和

const
    array = [{ key: 0, x: [], y: [] }, { key: 0, x: [], y: [] }],
    sum = array.reduce((s, { key, ...o }) =>
        Object.values(o).reduce((t, { length }) => t + length, s), 0);

console.log(sum);

【讨论】:

    【解决方案2】:

    reduce 这样做有点笨拙,而且您循环遍历数组中的项目,而不是(我认为)您期望的键。

    改用Array.every

    const arr = [    
        {key: 0, x: [], y: []},
        {key: 0, x: [], y: []}
    ]
    
    const arr2 = [    
        {key: 0, x: [1,2,3], y: []},
        {key: 0, x: [], y: []}
    ]
    
    function areAllEmpty(a){
        return a.every(i => i.x.length == 0 && i.y.length == 0); 
    }
    
    console.log(areAllEmpty(arr));
    console.log(areAllEmpty(arr2));

    【讨论】:

      【解决方案3】:

      因为acc 将是数组中的对象,所以您应该检查对象属性是数组。为此使用Object.values,并为每个对象内的数组使用另一个reduce,并排除key

      const arr = [    
          {key: 0, x: [], y: []},
          {key: 0, x: [], y: []}
      ];
      
      const res = arr.reduce((a, { key, ...c }) => a + Object.values(c).reduce((e, { length: f }) => e + f, 0), 0);
      console.log(res);

      【讨论】:

        【解决方案4】:

        您可以在Object.values 上使用filter() 来仅获取数组值。然后flat() 并将长度添加到结果

        const arr = [    
            {key: 0, x: [], y: []},
            {key: 0, x: [], y: []}
        ]
        
        let res = arr.reduce((count, acc) => {
            return count + Object.values(acc).filter(x => x.constructor === Array).flat().length
        }, 0)
        
        console.log(res)

        【讨论】:

          【解决方案5】:

          Array.every 是更好的选择。您可以遍历对象的所有值并测试它们是否是数组,如果是,则返回 false 如果它们不为空。 every在找到非空数组后立即停止,或结束迭代,以先到者为准。

          const arr = [    
            {key: 0, x: [], y: []},
            {key: 0, x: [], y: []}
          ];
          
          const arr2 = [    
            {key: 0, x: [], y: [1]},
            {key: 0, x: [], y: []}
          ];
          
          const allEmpty = (arr) => arr.every(el => {
            for (let v of Object.values(el)) {
              if (Array.isArray(v) && v.length > 0) return false;
            }
            return true;
          });
          
          console.log(allEmpty(arr));
          console.log(allEmpty(arr2));

          【讨论】:

            【解决方案6】:

            使用Array.reduce()在对象中求和仅数组的长度

            const arr = [    
                {key: 0, x: [], y: [], abc: "hsgg" },
                {key: 0, x: [1, 2, 3, 4], y: [1, 2]}
            ];
            
            const c = arr.reduce((count, obj) => {
                Object.values(obj).forEach(v => {
                  if(Array.isArray(v)) count += v.length;
                });
                return count;
            }, 0);
            
            console.log(c);

            请注意,在其他答案中字符串的长度也被计算在内

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-04-21
              • 2021-12-16
              • 2022-10-13
              • 2017-08-04
              • 1970-01-01
              • 2011-08-01
              相关资源
              最近更新 更多