DATA IN DATABASE
"data": [
{
"recruiter": "Venkatesh",
"Total": [
{
"_id": "6368de60a13476be793928bb",
"Status": "Client Submitted",
"created_by": "Venkatesh"
},
{
"_id": "636b71192fdb6190cd3ac4ab",
"created_by": "Venkatesh",
"Status": "Hired"
},
{
"_id": "636b941b2fdb6190cd3acd15",
"created_by": "Venkatesh",
"Status": "Interview Scheduled"
},
{
"_id": "636cd69d2fdb6190cd3b1be2",
"created_by": "Venkatesh",
"Status": "Client Submitted"
},
{
"_id": "6372301975e1e77a9c3b5896",
"Status": "Client Submitted",
"created_by": "Venkatesh"
},
{
"_id": "637761ed655965f094779322",
"created_by": "Venkatesh",
"Status": "Hired"
},
{
"_id": "637b04f5655965f094779d40",
"created_by": "Venkatesh",
"Status": "Hired"
}
]
},
{
"recruiter": "Sudhir",
"Total": [
{
"_id": "636b73f42fdb6190cd3ac765",
"created_by": "Sudhir",
"Status": "Client Submitted"
},
{
"_id": "6371efd059a6b9f34f910527",
"created_by": "Sudhir",
"Status": "Hired"
},
{
"_id": "63724e7c75e1e77a9c3b5cb7",
"created_by": "Sudhir",
"Status": "Client Submitted"
},
{
"_id": "6373210c3182820f833b41a4",
"created_by": "Sudhir",
"Status": "Interview Scheduled"
},
{
"_id": "637332423182820f833b493b",
"created_by": "Sudhir",
"Status": "Client Submitted"
}
]
},
RESPONSE I WANT
[
{
created_by: 'Venkatesh',
Hired: 7,
interviewscheduled: 7,
clientsubmitted: 7
},
{
created_by: 'Sudhir',
Hired: 5,
interviewscheduled: 5,
clientsubmitted: 5
},
]
CODE
for(let i=0;i<=fil.length-1;i ){
let x={hired:[],is:[],cs:[]}
for(let j=0;j<=fil[i].Total.length-1;j ){
if(fil[i].Total[j].Status="hired"){
// h.push({hired:fil[i].Total[j].Status})
x.hired.push("hired")
}
if(fil[i].Total[j].Status="Interview Scheduled"){
// is.push({interviewscheduled:fil[i].Total[j].Status})
x.is.push("is")
}
if(fil[i].Total[j].Status="Client Submitted"){
// cs.push({cs:fil[i].Total[j].Status})
x.cs.push("cs")
}
} fil_arr.push({created_by:fil[i].recruiter,Hired:x.hired.length,interviewscheduled:x.is.length,clientsubmitted:x.cs.length})
}
I want to show the total number of interviewscheduled, clientsubmitted,hired for status key based on recruiter.Iam unable to find the correct output for this.I tried using for loop but it is not giving proper output.please do help me regarding this.Thank you in advance.
CodePudding user response:
A simple mapping from status
value to property
name of your final output can make the task easier.
I would try like this:
const data = [ { recruiter: "Venkatesh", Total: [ { _id: "6368de60a13476be793928bb", Status: "Client Submitted", created_by: "Venkatesh", }, { _id: "636b71192fdb6190cd3ac4ab", created_by: "Venkatesh", Status: "Hired", }, { _id: "636b941b2fdb6190cd3acd15", created_by: "Venkatesh", Status: "Interview Scheduled", }, { _id: "636cd69d2fdb6190cd3b1be2", created_by: "Venkatesh", Status: "Client Submitted", }, { _id: "6372301975e1e77a9c3b5896", Status: "Client Submitted", created_by: "Venkatesh", }, { _id: "637761ed655965f094779322", created_by: "Venkatesh", Status: "Hired", }, { _id: "637b04f5655965f094779d40", created_by: "Venkatesh", Status: "Hired", }, ], }, { recruiter: "Sudhir", Total: [ { _id: "636b73f42fdb6190cd3ac765", created_by: "Sudhir", Status: "Client Submitted", }, { _id: "6371efd059a6b9f34f910527", created_by: "Sudhir", Status: "Hired", }, { _id: "63724e7c75e1e77a9c3b5cb7", created_by: "Sudhir", Status: "Client Submitted", }, { _id: "6373210c3182820f833b41a4", created_by: "Sudhir", Status: "Interview Scheduled", }, { _id: "637332423182820f833b493b", created_by: "Sudhir", Status: "Client Submitted", }, ], }, ];
const StatusMap = {
"Client Submitted": "clientsubmitted",
"Interview Scheduled": "interviewscheduled",
Hired: "Hired",
};
const output = data.map(({ recruiter, Total }) => {
return {
created_by: recruiter,
...Total.reduce(
(prev, curr) => {
prev[StatusMap[curr.Status]] = 1;
return prev;
},
{ Hired: 0, interviewscheduled: 0, clientsubmitted: 0 }
),
};
});
console.log(output);
NOTE: Sometimes it's better to use Array.prototype.map() and Array.prototype.reduce() like array builtin functions.
CodePudding user response:
Here is a solution using Map:
const mapUser = new Map();
const keyValue = {
"Client Submitted": "clientsubmitted",
"Interview Scheduled": "interviewscheduled",
"Hired": "Hired"
}
data.forEach(recruiter => {
recruiter['Total'].forEach(user => {
if (mapUser.has(user.created_by)) {
const value = mapUser.get(user.created_by)[keyValue[user["Status"]]]
mapUser.get(user.created_by)[keyValue[user["Status"]]] = (value || 0) 1
} else {
mapUser.set(user.created_by, {[keyValue[user["Status"]]]: 1})
}
})
})
const result = Array.from(mapUser).map(([k,v]) => {
return {created_by: k, ...v}
})
Be careful, you should think about harmonize case of your properties, like all camelCase or snake_case for instance.
Also, you duplicate the property created_by, as soon as you have the info in the recruiter property above.