I am designing a Dashboard using Node.js which measures peoples targets. I currently have the backend set up and new targets can be added.
So some example data from MongoDB:
var = targets = [
{
_id: ObjectId("53e0d2e340db30cb151413d4"),
owner: "Some Guy",
objective: "Make coffee",
metric: "Availability",
rag: "Amber"
},
{
_id: ObjectId("53e0d2e340db30cb151413d4"),
owner: "Someone Else",
objective: "Answer phones",
metric: "Blah",
rag: "Green"
},
{
_id: ObjectId("53e0d2e340db30cb151413d4"),
owner: "Some Guy",
objective: "Clean desks",
metric: "Wah",
rag: "Red"
}
];
To remove duplicate owners to display a list of tabs I have the following code using EJS and Underscore.js:
<% _.each(targets, function(target) { %>
<% if ((arr.indexOf(target.owner)) === -1) { %>
<% arr.push(target.owner) %>
<% } %>
<% }) %>
Which produces the following posts data to a carousel which has a tab for each owner. In the tab then there is an option to click between the multiple targets for that owner.
Each owner can have multiple targets so when their tab is selected I can swipe through the carousels. What I can not figure out is how I am going to to take each target from a specific owner and add all the targets under their tab and so on for other owners.
My thinking was to create a new array for each owner an then loop through all the targets adding the specific target to the correct owner array. So I have made a start on this but I am not sure how to push to a specific array or if this is even the correct way of doing it?
<% _.each(arr, function(owner, i) { %>
<% this['targets_' + i] = [] %>
<% _.each(targets, function(target) { %>
<%if (target.owner === owner) {%>
<%(['targets_' + i]).push(target.id)%>
<% console.log(target.id) %>
<%}%>
<% }) %>
<% } %>