簡體   English   中英

使用lodash按javascript中的對象數組中的值進行計數

[英]count by value in array of objects in javascript using lodash

我有一個對象數組,如下所示:

var input_array = [{
    role_name: 'Full Stack Developer',
    position_id: 'b0f00e68-5adc-4209-aec2-9c4962550ab1',
    email_address: 'abc.recruiter@gmail.com',
    application_id: '1dd45634-c283-4a96-a28a-d8a63c418329',
    state: 'qualified',
    closing_date: '2018-10-28 06:30:00' },
{
    role_name: 'Delivery Representative',
    position_id: '090276f0-3fca-4b2a-85ed-697d21c405a0',
    email_address: 'abc.recruiter@gmail.com',
    application_id: 'aa7fe2dd-b141-4c64-8350-a1d57bfaa502',
    state: 'interview',
    closing_date: '2018-10-28 06:30:00' },
{
    role_name: 'Delivery Representative',
    position_id: '12345678-5adc-4209-aec2-9c4962550ab1',
    email_address: 'xyz.recruiter@gmail.com',
    application_id: '166da0ac-aaf1-400d-9a62-e37962f66653',
    state: 'interview',
    closing_date: '2018-10-28 06:30:00' },
{
    role_name: 'Delivery Representative',
    position_id: '090276f0-3fca-4b2a-85ed-697d21c405a0',
    email_address: 'abc.recruiter@gmail.com',
    application_id: 'da09a617-8e82-43c0-b1ea-725110a2c4cb',
    state: 'interview',
    closing_date: '2018-10-28 06:30:00' },
{
    role_name: 'Delivery Representative',
    position_id: '12345678-5adc-4209-aec2-9c4962550ab1',
    email_address: 'xyz.recruiter@gmail.com',
    application_id: '1d55a51a-ecd1-43ea-9cf4-fed101dbebda',
    state: 'offer',
    closing_date: '2018-10-28 06:30:00' },
{
    role_name: 'Micro Space Planner',
    position_id: 'fe30930f-7d9f-4953-8939-6d9924462b2b',
    email_address: 'abc.recruiter@gmail.com',
    application_id: '293bd084-64f0-4c83-9b5d-aa304e44f066',
    state: 'screening',
    closing_date: '2018-10-28 06:30:00' },
{
    role_name: 'Delivery Representative',
    position_id: '12345678-5adc-4209-aec2-9c4962550ab1',
    email_address: 'xyz.recruiter@gmail.com',
    application_id: '2adc5236-989d-49f2-ab3e-cb7e42798b77',
    state: 'qualified',
    closing_date: '2018-10-28 06:30:00' },
{
    role_name: 'Micro Space Planner',
    position_id: 'fe30930f-7d9f-4953-8939-6d9924462b2b',
    email_address: 'abc.recruiter@gmail.com',
    application_id: '293bd084-64f0-4c83-9b5d-aa304e44f066',
    state: 'screening',
    closing_date: '2018-10-28 06:30:00' },
{
    role_name: 'Delivery Representative',
    position_id: '12345678-5adc-4209-aec2-9c4962550ab1',
    email_address: 'xyz.recruiter@gmail.com',
    application_id: '2adc5236-989d-49f2-ab3e-cb7e42798b77',
    state: 'qualified',
    closing_date: '2018-10-28 06:30:00' }]

我想把與每個職位相對應的狀態作為要約和面試數。 我正在使用npm lodash庫。

我已經檢查了關於分組依據的一些問題,並使用過濾器進行了計數,但在我的情況下,職位ID是動態的。 因此,我無法設置用於對對象進行分組的position_id的特定值。

例如,position_id為'12345678-5adc-4209-aec2-8b3962550ce7'包含1個報價和1個面試

預期產量:

{
    role_name: 'Delivery Representative',
    position_id: '12345678-5adc-4209-aec2-9c4962550ab1',
    email_address: 'xyz.recruiter@gmail.com',
    offer_count: 1,
    interview_count: 1
},
{
    role_name: 'Micro Space Planner',
    position_id: 'fe30930f-7d9f-4953-8939-6d9924462b2b',
    email_address: 'abc.recruiter@gmail.com',
    offer_count: 0,
    interview_count:0
},
{
    role_name: 'Delivery Representative',
    position_id: '090276f0-3fca-4b2a-85ed-697d21c405a0',
    email_address: 'abc.recruiter@gmail.com',
    offer_count: 0,
    interview_count:2
}

您可以將reducemap結合使用,可以是本機JS或lodash版本。 首先,通過位置ID減少記錄的數量,然后映射到對象條目上以生成所需的數組。

Object.entries(input_arr.reduce((acc, item) => {
  if (!acc[item.position_id]) {
    acc[item.position_id] = {
      interview_count: 0,
      offer_count: 0,
      email_address: item.email_address,
      role_name: item.role_name
    };
  }

  if (item.state === 'interview') acc[item.position_id].interview_count += 1;
  if (item.state === 'offer') acc[item.position_id].offer_count += 1;
  return acc;
}, {})).map(([position_id, {email_address, interview_count, offer_count, role_name}]) => {
  return {
    position_id,
    email_address,
    interview_count,
    offer_count,
    role_name
  };
});

這是一個小提琴

您可以將原始數組簡化為一個新的哈希對象,以保存位置圖並計算輸入數組的狀態。

let interviews = input_array.reduce(function(acc, cur) {  // go over every applicant
    if (!acc[cur.position_id]) {
        acc[cur.position_id] = {      //set initial object if the job is new
            role_name: cur.role_name,
            ...,
            offer_count: 0,
            interview_count:0
        }
    }

    if (cur.state === "offer") {
        acc[cur.position_id].offer_count++;
    } else if (cur.state === "interview") {
        acc[cur.position_id].interview_count++;
    }

    return acc;
}, {});

Reduce是本機JS數組方法,因此您實際上不需要使用lodash或任何外部庫進行此計算

您可以使用函數reduce來按id對對象進行分組,並使用Object.values函數來提取分組的對象。

 let input_array = [{ role_name: 'Full Stack Developer', position_id: 'b0f00e68-5adc-4209-aec2-9c4962550ab1', email_address: 'abc.recruiter@gmail.com', application_id: '1dd45634-c283-4a96-a28a-d8a63c418329', state: 'qualified', closing_date: '2018-10-28 06:30:00' },{ role_name: 'Delivery Representative', position_id: '090276f0-3fca-4b2a-85ed-697d21c405a0', email_address: 'abc.recruiter@gmail.com', application_id: 'aa7fe2dd-b141-4c64-8350-a1d57bfaa502', state: 'interview', closing_date: '2018-10-28 06:30:00' },{ role_name: 'Delivery Representative', position_id: '12345678-5adc-4209-aec2-9c4962550ab1', email_address: 'xyz.recruiter@gmail.com', application_id: '166da0ac-aaf1-400d-9a62-e37962f66653', state: 'interview', closing_date: '2018-10-28 06:30:00' },{ role_name: 'Delivery Representative', position_id: '090276f0-3fca-4b2a-85ed-697d21c405a0', email_address: 'abc.recruiter@gmail.com', application_id: 'da09a617-8e82-43c0-b1ea-725110a2c4cb', state: 'interview', closing_date: '2018-10-28 06:30:00' },{ role_name: 'Delivery Representative', position_id: '12345678-5adc-4209-aec2-9c4962550ab1', email_address: 'xyz.recruiter@gmail.com', application_id: '1d55a51a-ecd1-43ea-9cf4-fed101dbebda', state: 'offer', closing_date: '2018-10-28 06:30:00' },{ role_name: 'Micro Space Planner', position_id: 'fe30930f-7d9f-4953-8939-6d9924462b2b', email_address: 'abc.recruiter@gmail.com', application_id: '293bd084-64f0-4c83-9b5d-aa304e44f066', state: 'screening', closing_date: '2018-10-28 06:30:00' },{ role_name: 'Delivery Representative', position_id: '12345678-5adc-4209-aec2-9c4962550ab1', email_address: 'xyz.recruiter@gmail.com', application_id: '2adc5236-989d-49f2-ab3e-cb7e42798b77', state: 'qualified', closing_date: '2018-10-28 06:30:00' },{ role_name: 'Micro Space Planner', position_id: 'fe30930f-7d9f-4953-8939-6d9924462b2b', email_address: 'abc.recruiter@gmail.com', application_id: '293bd084-64f0-4c83-9b5d-aa304e44f066', state: 'screening', closing_date: '2018-10-28 06:30:00' },{ role_name: 'Delivery Representative', position_id: '12345678-5adc-4209-aec2-9c4962550ab1', email_address: 'xyz.recruiter@gmail.com', application_id: '2adc5236-989d-49f2-ab3e-cb7e42798b77', state: 'qualified', closing_date: '2018-10-28 06:30:00' }], result = Object.values(input_array.reduce((a, {state, role_name, position_id, email_address}) => { a[position_id] = (a[position_id] || {role_name, position_id, email_address, offer_count: 0, interview_count: 0}); a[position_id].offer_count += (state === 'offer'); a[position_id].interview_count += (state === 'interview'); return a; }, Object.create(null))); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

您可以使用不帶lodash的類似方法執行此操作,並通過reduceObject.assignObject.values

 var data = [{ role_name: 'Full Stack Developer', position_id: 'b0f00e68-5adc-4209-aec2-9c4962550ab1', email_address: 'abc.recruiter@gmail.com', application_id: '1dd45634-c283-4a96-a28a-d8a63c418329', state: 'qualified', closing_date: '2018-10-28 06:30:00' }, { role_name: 'Delivery Representative', position_id: '090276f0-3fca-4b2a-85ed-697d21c405a0', email_address: 'abc.recruiter@gmail.com', application_id: 'aa7fe2dd-b141-4c64-8350-a1d57bfaa502', state: 'interview', closing_date: '2018-10-28 06:30:00' }, { role_name: 'Delivery Representative', position_id: '12345678-5adc-4209-aec2-9c4962550ab1', email_address: 'xyz.recruiter@gmail.com', application_id: '166da0ac-aaf1-400d-9a62-e37962f66653', state: 'interview', closing_date: '2018-10-28 06:30:00' }, { role_name: 'Delivery Representative', position_id: '090276f0-3fca-4b2a-85ed-697d21c405a0', email_address: 'abc.recruiter@gmail.com', application_id: 'da09a617-8e82-43c0-b1ea-725110a2c4cb', state: 'interview', closing_date: '2018-10-28 06:30:00' }, { role_name: 'Delivery Representative', position_id: '12345678-5adc-4209-aec2-9c4962550ab1', email_address: 'xyz.recruiter@gmail.com', application_id: '1d55a51a-ecd1-43ea-9cf4-fed101dbebda', state: 'offer', closing_date: '2018-10-28 06:30:00' }, { role_name: 'Micro Space Planner', position_id: 'fe30930f-7d9f-4953-8939-6d9924462b2b', email_address: 'abc.recruiter@gmail.com', application_id: '293bd084-64f0-4c83-9b5d-aa304e44f066', state: 'screening', closing_date: '2018-10-28 06:30:00' }, { role_name: 'Delivery Representative', position_id: '12345678-5adc-4209-aec2-9c4962550ab1', email_address: 'xyz.recruiter@gmail.com', application_id: '2adc5236-989d-49f2-ab3e-cb7e42798b77', state: 'qualified', closing_date: '2018-10-28 06:30:00' }, { role_name: 'Micro Space Planner', position_id: 'fe30930f-7d9f-4953-8939-6d9924462b2b', email_address: 'abc.recruiter@gmail.com', application_id: '293bd084-64f0-4c83-9b5d-aa304e44f066', state: 'screening', closing_date: '2018-10-28 06:30:00' }, { role_name: 'Delivery Representative', position_id: '12345678-5adc-4209-aec2-9c4962550ab1', email_address: 'xyz.recruiter@gmail.com', application_id: '2adc5236-989d-49f2-ab3e-cb7e42798b77', state: 'qualified', closing_date: '2018-10-28 06:30:00' }] const combine = (r = {offer_count: 0, interview_count: 0}, c) => { r.offer_count += c.state ==='offer' r.interview_count += c.state === 'interview' let {application_id, state, closing_date, ...rest} = c return Object.assign(r, rest) } const countEm = d => Object.values(d.reduce((r,c) => (r[c.position_id] = combine(r[c.position_id], c), r), {})) console.log(countEm(data)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM