user3679330
user3679330

Reputation: 139

Lodash _.filter - filter array to return most occurences

I have the following array:

{ 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' }

I am using Lodash _.filter() function to try and sort the following array to return only the elements that have value equal to the most occurences of revenue, industry_id and employees.

In this case the number that appears the most for these (based off looking at the array )

Is revenue: 0, industry_id: 0 and employees: 0

How would I filter the array to do this with _.filter? I haven't managed to figure out a way to do this.

Thanks.

Upvotes: 0

Views: 215

Answers (3)

Ori Drori
Ori Drori

Reputation: 192287

Create an "id" for each item by combining the revenue, industry_id and employees values, count the occurrences in the data, and get the "id" of the highest one.

Filter the data using this 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>

Upvotes: 0

Rick
Rick

Reputation: 1055

You can use a map to keep a tally of occurrences.

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));

Upvotes: 0

shawon191
shawon191

Reputation: 1955

You need to find the most frequent values of necessary fields first. You can check this to find out how to do that.

Once you have the most frequent fields you can do this -

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

Upvotes: 1

Related Questions