I have an array of objects which is having individual task and I need to filter out the each task according to employee id and then project id and after this I need to check if the sum of hours in the resulting array is less than 4. If all these conditions satisfy then store those values in the array. This is the response from api.
nonallocated: [
{
empId: "1",
task: {
project: {
projectId: "111"
},
total: '2'
}
},
{
empId: "2",
task: {
project: {
projectId: "111"
},
total: '5'
}
},
{
empId: "2",
task: {
project: {
projectId: "333"
},
total: '1'
}
},
{
empId: "3",
task: {
project: {
projectId: "111"
},
total: '2'
}
},
{
empId: "3",
task: {
project: {
projectId: "111"
},
total: '1'
}
},
{
empId: "1",
task: {
project: {
projectId: "222"
},
total: '5'
}
},
{
empId: "1",
task: {
project: {
projectId: "111"
},
total: '1'
}
}
]
expected output:
newArr = [
//matching empId(1), projectId(111), total is less than 4 i.e 2 1=3
{
empId: "1",
task: {
project: {
projectId: "111"
},
total: '2'
}
},
{
empId: "1",
task: {
project: {
projectId: "111"
},
total: '1'
}
},
// having only one data for same empID and projectId and total is less than 4
{
empId: "2",
task: {
project: {
projectId: "333"
},
total: '1'
}
},
//having same empId(3) and projectId(111) and total is less than 4 ie
2 1 =3
{
empId: "3",
task: {
project: {
projectId: "111"
},
total: '2'
}
},
{
empId: "3",
task: {
project: {
projectId: "111"
},
total: '1'
}
},
]
this should not be limited to one empId or projectId. I want these conditions to satisfy on any data for example we can have a data which has same conditions satisfied for empId 2. I want to retrieve the data for values which have same employee Id, same projectId and total less than 4. and if I have a data which doesn't have empId and projectId matched with other data just check the total less than 4 in that case
CodePudding user response:
const nonallocated = [
{
empId: "1",
task: {
project: {
projectId: "111"
},
total: '2'
}
},
{
empId: "2",
task: {
project: {
projectId: "111"
},
total: '3'
}
},
{
empId: "1",
task: {
project: {
projectId: "222"
},
total: '1'
}
},
{
empId: "1",
task: {
project: {
projectId: "111"
},
total: '1'
}
}
]
// Filter Array Based On EmpId And ProjectId And Total Less Than Four
const filterArr = (empId = "", proId = "") => nonallocated.filter(n => n.empId === empId && n.task.project.projectId === proId && n.task.total < 4);
const newArr = filterArr("1", "111");
console.log(newArr);
CodePudding user response:
Something like this - just use filter on the array and make a match on what you want to match on:
const nonallocate = [
{
empId: "1",
task: {
project: {
projectId: "111"
},
total: '2'
}
},
{
empId: "2",
task: {
project: {
projectId: "111"
},
total: '3'
}
},
{
empId: "1",
task: {
project: {
projectId: "222"
},
total: '1'
}
},
{
empId: "1",
task: {
project: {
projectId: "111"
},
total: '1'
}
}
]
const result = nonallocate.filter(item => {
const total = item?.task?.total || 0
return item.empId === '1' && parseInt(total, 10) <= 4
})
console.log(result)
CodePudding user response:
Posting my approach:
var proj="111";
var empl="1";
var filtered=nonallocated.filter(e => e.empId===empl && e.task && e.task.project && e.task.project.projectId===proj && e.task.total>0);
var tasks=filtered.map(e => parseInt(e.task.total, 10));
var total = tasks.reduce((a, b) => a b, 0);
console.log(JSON.stringify(filtered))
console.log(JSON.stringify(tasks))
console.log(total);