【问题标题】:Lodash _.filter - filter array to return most occurencesLodash _.filter - 过滤数组以返回大多数出现
【发布时间】:2017-11-29 23:57:44
【问题描述】:

我有以下数组:

{ id: 19531285,
   domain: 'fjuhsd.org',
   alexa_rank: 458835,
   country: 236,
   employees: '0',
   revenue: '0',
   industry_id: '0' },
 { id: 2657031,
   domain: 'deporlovers.
   alexa_rank: 470687,
   country: 209,
   employees: '0',
   revenue: '0',
   industry_id: '0' },
 { id: 1846092,
   domain: 'lehighsports
   alexa_rank: 477376,
   country: 236,
   employees: '0',
   revenue: '0',
   industry_id: '0' },
 { id: 41443846,
   domain: 'blacklightsl
   alexa_rank: 477964,
   country: 0,
   employees: '0',
   revenue: '0',
   industry_id: '0' },
 { id: 3881608,
   domain: 'audubonportl
   alexa_rank: 478643,
   country: 236,
   employees: '2',
   revenue: '2',
   industry_id: '39' },
 { id: 32704527,
   domain: 'lowryparkzoo
   alexa_rank: 488859,
   country: 236,
   employees: '0',
   revenue: '0',
   industry_id: '0' },
 { id: 1907473,
   domain: 'citymb.info'
   alexa_rank: 490285,
   country: 236,
   employees: '4',
   revenue: '4',
   industry_id: '53' },
 { id: 8716166,
   domain: 'terrainracin
   alexa_rank: 490404,
   country: 0,
   employees: '0',
   revenue: '0',
   industry_id: '0' },
 { id: 935439,
   domain: 'triatlonchan
   alexa_rank: 491953,
   country: 83,
   employees: '0',
   revenue: '0',
   industry_id: '0' }

我正在使用 Lodash _.filter() 函数尝试对以下数组进行排序,以仅返回值等于收入、行业 ID 和雇员出现次数最多的元素。

在这种情况下,出现最多的数字(基于查看数组)

是收入:0,行业 ID:0 和员工:0

我将如何过滤数组以使用 _.filter 执行此操作?我还没有想办法做到这一点。

谢谢。

【问题讨论】:

    标签: arrays node.js lodash


    【解决方案1】:

    您需要首先找到必要字段的最常见值。您可以查看this 了解如何操作。

    一旦你拥有最常用的字段,你就可以这样做 -

    _.filter(records, record => {
        return record.revenue === mostFrequentRevenue &&
            record.industry_id === mostFrequentIndustryId &&
            record.employees === mostFrequentEmployees;
    })
    

    【讨论】:

      【解决方案2】:

      您可以使用地图来记录发生次数。

      function sortExtract(data) {
        var sorted = data.sort((a, b) => {
          return a.revenue > b.revenue || a.industry_id > b.industry_id || a.employees > b.employees;
        })
      
        var count = new Map();
        for (let l of sorted) {
          var m = l.revenue + l.employees;
          if (count.has(m)) {
            var val = count.get(m);
            val.push(l)
          } else count.set(m, [l]);
        }
        var len = 0;
        var obj = 0
        var len = Array.from(count, (x, y) => {
          if (x[1].length > len) {
            len = x[1].length;
            obj = x[1]
          }
        })
        return obj[0];
      }
      
      var data = [{
          id: 19531285,
          domain: 'fjuhsd.org',
          alexa_rank: 458835,
          country: 236,
          employees: '0',
          revenue: '0',
          industry_id: '0'
        },
        {
          id: 2657031,
          domain: 'deporlovers',
          alexa_rank: 470687,
          country: 209,
          employees: '0',
          revenue: '0',
          industry_id: '0'
        },
        {
          id: 1846092,
          domain: 'lehighsport',
          alexa_rank: 477376,
          country: 236,
          employees: '0',
          revenue: '0',
          industry_id: '0'
        },
        {
          id: 41443846,
          domain: 'blacklights',
          alexa_rank: 477964,
          country: 0,
          employees: '0',
          revenue: '0',
          industry_id: '0'
        },
        {
          id: 3881608,
          domain: 'audubonport',
          alexa_rank: 478643,
          country: 236,
          employees: '2',
          revenue: '2',
          industry_id: '39'
        },
        {
          id: 32704527,
          domain: 'lowryparkzoo',
          alexa_rank: 488859,
          country: 236,
          employees: '0',
          revenue: '0',
          industry_id: '0'
        },
        {
          id: 1907473,
          domain: 'citymb.info',
          alexa_rank: 490285,
          country: 236,
          employees: '4',
          revenue: '4',
          industry_id: '53'
        },
        {
          id: 8716166,
          domain: 'terrainraci',
          alexa_rank: 490404,
          country: 0,
          employees: '0',
          revenue: '0',
          industry_id: '0'
        },
        {
          id: 935439,
          domain: 'triatloncha',
          alexa_rank: 491953,
          country: 83,
          employees: '0',
          revenue: '0',
          industry_id: '0'
        }
      ];
      
      console.log(sortExtract(data));

      【讨论】:

        【解决方案3】:

        通过结合收入、行业ID和员工值,为每个项目创建一个“id”,计算数据中出现的次数,并获得最高的“id”。

        使用此 id 过滤数据。

        const data = [{"id":19531285,"domain":"fjuhsd.org","alexa_rank":458835,"country":236,"employees":"0","revenue":"0","industry_id":"0"},{"id":2657031,"domain":"deporlovers","alexa_rank":470687,"country":209,"employees":"0","revenue":"0","industry_id":"0"},{"id":1846092,"domain":"lehighsport","alexa_rank":477376,"country":236,"employees":"0","revenue":"0","industry_id":"0"},{"id":41443846,"domain":"blacklights","alexa_rank":477964,"country":0,"employees":"0","revenue":"0","industry_id":"0"},{"id":3881608,"domain":"audubonport","alexa_rank":478643,"country":236,"employees":"2","revenue":"2","industry_id":"39"},{"id":32704527,"domain":"lowryparkzoo","alexa_rank":488859,"country":236,"employees":"0","revenue":"0","industry_id":"0"},{"id":1907473,"domain":"citymb.info","alexa_rank":490285,"country":236,"employees":"4","revenue":"4","industry_id":"53"},{"id":8716166,"domain":"terrainraci","alexa_rank":490404,"country":0,"employees":"0","revenue":"0","industry_id":"0"},{"id":935439,"domain":"triatloncha","alexa_rank":491953,"country":83,"employees":"0","revenue":"0","industry_id":"0"}];
        
        const getId = ({ revenue, industry_id, employees }) => `${revenue}-${industry_id}-${employees}`; // create the id of the element by combining the required properties
        
        const mostOccurring = _.get(_(data)
          .countBy(getId) // count the number of items that have the same "id"
          .entries() // get the entries
          .maxBy(([_, v]) => v), 0); // find the maximum, and use get to take the id
          
        const result = data.filter((o) => getId(o) === mostOccurring); // filter all items by the most occuring id
        
        console.log(result);
        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-05
          • 2018-09-02
          • 1970-01-01
          • 2021-11-29
          • 2018-03-26
          • 1970-01-01
          • 1970-01-01
          • 2020-05-04
          相关资源
          最近更新 更多