2017-06-27 14 views
0

を返すためにI次の配列があります。Lodash _.filter - フィルタアレイは、最も出現箇所

{ 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()関数を使用していますが、その最も多くの収益、industry_id、従業員に等しい価値を持ちます。

この場合、(配列を見てオフに基づいて)これらのために最も出現回数が

は収入です:0、industry_id:0と従業:0

どのように私はに配列をフィルタリングします_.filterでこれを行いますか?私はこれを行う方法を見つけ出すことができませんでした。

ありがとうございました。

答えて

1

まず、必要なフィールドの値を最も頻繁に検索する必要があります。 thisをチェックして、その方法を調べることができます。あなたが最も頻繁にフィールドを持っていたら

あなたはこれを行うことができます -

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

あなたは出現の集計を維持するためにマップを使用することができます。

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

0

、収入、industry_idと従業員の値を組み合わせることにより、各項目の「ID」を作成したデータで発生をカウントし、最高の1の「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>

関連する問題