Reputation: 6017
I'm in the process of building a portfolio, one project I'd like to build is something similar to AirTasker
,
I'm currently working on API
built in node.js
which takes in a JSON
object, this JSON
object is basically tasks that are being requested and need to be completed by the relevant person it is assigned to at present it's automatically assigned.
This is my test dummy data:
[
{
"clientId": 1,
"requestId": "help move bed",
"hours": 6
},
{
"clientId": 2,
"requestId": "pickup parcel",
"hours": 1
},
{
"clientId": 1,
"requestId": "cut the grass",
"hours": 4
},
{
"clientId": 1,
"requestId": "clean the kitchen",
"hours": 2
}
]
now my API
is currently structured as follows:
var workers = [
{"id": 1, "name": "Person 1", "tasks": []},
{"id": 2, "name": "Person 2", "tasks": []},
{"id": 3, "name": "Person 3", "tasks": []},
{"id": 4, "name": "Person 4", "tasks": []},
{"id": 5, "name": "Person 5", "tasks": []},
{"id": 6, "name": "Person 6", "tasks": []},
]
app.post('/api/v1/request', (req, res) => {
var requestData = req.body;
// Loop the tasks passed in
requestData.forEach(request => {
// Now evenly spread tasks across workers
workers.forEach(p => {
workers.tasks.push(request);
})
console.log(request);
});
res.status(200).send({
success: 'true',
message: 'Tasks has been assigned.',
workers: workers
})
});
the object workers
as the people who will be doing the tasks, the issue I have is when I loop requestData
I can't figure out the syntax to assign one task per one worker, at the moment how my code is it'll assign the one task to all workers and continue, the end result will be all workers have all the same tasks.
Can someone assist me with how I go about assigning one unique task per each worker
Upvotes: 0
Views: 95
Reputation: 3057
You can use the modulus function to help.
var workers = [
{"id": 1, "name": "Person 1", "tasks": []},
{"id": 2, "name": "Person 2", "tasks": []},
{"id": 3, "name": "Person 3", "tasks": []},
{"id": 4, "name": "Person 4", "tasks": []},
{"id": 5, "name": "Person 5", "tasks": []},
{"id": 6, "name": "Person 6", "tasks": []},
]
var tasks = [
{
name: 'a',
hours: 8
},
{
name: 'b',
hours: 4
},
{
name: 'c',
hours: 2
},{
name: 'd',
hours: 4
},
{
name: 'e',
hours: 3
},
{
name: 'f',
hours: 5
},
{
name: 'g',
hours: 4
},
{
name: 'a',
hours: 2
},
]
var reducer = (acc, val) => acc + val.hours;
var length = workers.length;
var start = 0;
tasks.forEach(task => {
if (workers[start%length].tasks.reduce(reducer, 0) < 8) {
workers[start%length].tasks.push(task);
}
start += 1;
});
console.log(workers)
Here's a demo:
Upvotes: 1
Reputation: 308
You should add task to single worker object in foreach loop like this:
workers.forEach(butler => {
butler.tasks.push(request);
})
Upvotes: 1
Reputation: 769
const workers = [
{"id": 1, "name": "Person 1", "tasks": []},
{"id": 2, "name": "Person 2", "tasks": []},
{"id": 3, "name": "Person 3", "tasks": []},
{"id": 4, "name": "Person 4", "tasks": []},
{"id": 5, "name": "Person 5", "tasks": []},
{"id": 6, "name": "Person 6", "tasks": []},
]
const dummies = [
{
"clientId": 1,
"requestId": "help move bed",
"hours": 6
},
{
"clientId": 2,
"requestId": "pickup parcel",
"hours": 1
},
{
"clientId": 1,
"requestId": "cut the grass",
"hours": 4
},
{
"clientId": 1,
"requestId": "clean the kitchen",
"hours": 2
}
];
workers.forEach(w => {
w.tasks = dummies.filter(d => d.clientId === w.id);
});
console.log(workers);
Try this simple code.
Upvotes: 1