我有一个对象数组,如下所示:
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' }]
我想统计每个职位对应的offer和面试状态的对象。我正在使用 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
}
最佳答案
您可以使用函数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; }
关于javascript - 使用 lodash 在 javascript 中按对象数组中的值进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52989098/