title: Grouping targets under one owner from an array of objects
body: <p>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.</p>
  
  <p>So some example data from MongoDB:</p>
  
  <pre><code>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"
    }
  ];
  </code></pre>
  
  <p>To remove duplicate owners to display a list of tabs I have the following code using EJS and Underscore.js:</p>
  
  <pre><code>&lt;% _.each(targets, function(target) { %&gt;
    &lt;% if ((arr.indexOf(target.owner)) === -1) { %&gt;
      &lt;% arr.push(target.owner) %&gt;
    &lt;% } %&gt;
  &lt;% }) %&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <pre><code>&lt;% _.each(arr, function(owner, i) { %&gt;
    &lt;% this['targets_' + i] = [] %&gt;
    &lt;% _.each(targets, function(target) { %&gt;
      &lt;%if (target.owner === owner) {%&gt;
        &lt;%(['targets_' + i]).push(target.id)%&gt;
        &lt;% console.log(target.id) %&gt;
      &lt;%}%&gt;
    &lt;% }) %&gt;
  &lt;% } %&gt;
  </code></pre>