title: Better way to convert JSON to arrays for graphing timeseries in C3?
body: <p>I'm working on converting some JSON data into a format that will work for graphing a timeseries in C3, which takes an array of arrays as column input. I have my unique x values array, but I can't figure out a great way to get my y values. Once I get this done, I need to add y2 with numSold, but I think if I can solve this, I'll be able to figure that part out.</p>
  
  <p>This is the final format I need for the data:</p>
  
  <pre><code>[
    ["x", "9-2-17", "9-4-17", "10-2-17"],
    ['item1-cost', 100, 150, 10],
    ['item3-cost', 200, null, 20],
    ...
  ]
  </code></pre>
  
  <p>There isn't a set number of dates or set number of items. It's whatever I read from the database. The item numbers aren't necessarily consecutive. We only care about graphing the data for items we have data for. But there won't be data for every date-item combination. When that happens, I need to insert a null to indicate that the item didn't have data for that date. Item numbers are 1-indexed.</p>
  
  <p>I'm fine with using anything from lo-dash or D3 in addition to the plain JavaScript functions to solve this, but I'm trying to avoid hard to read code and inefficient code. I feel like others must have had a need for this and that there must be some sort of function like maybe a filter function that I should be using instead.</p>
  
  <p>My current implementation is not as efficient  as I'd like and is a bit hard to read. I'm using the item number as an index and making a sparse array that I'll have to condense later. Here's the psudocode:</p>
  
  <pre><code>For every d object in data
    For the i index of d.date in uniqueDates
      If values[d.item] is undefined
        Fill values[d.item]] with null for uniqueDates.length
      Set values[d.item][i] to d.cost
  Convert values to dense format for graphing
  </code></pre>
  
  <p>Here's a link to the Fiddle I'm playing with:</p>
  
  <p><a href="https://jsfiddle.net/dbkidd/q3r3moqu/" rel="nofollow noreferrer">https://jsfiddle.net/dbkidd/q3r3moqu/</a></p>
  
  <pre><code>var data = [
    {date: '9-2-17', item: 1, cost: 100},
    {date: '9-2-17', item: 3, cost: 200},
    {date: '9-4-17', item: 1, cost: 150},
    /* '9-4-17' does not have an entry for item 3 so cost should be counted as null */
    {date: '10-2-17', item: 1, cost: 10},
    {date: '10-2-17', item: 3, cost: 20}
  ]
  
  var uniqueDates = _.uniq(_.flatMap(data, 'date'));
  uniqueDates.unshift('x');
  
  var values = [];
  values.push(uniqueDates);
  
  function getLabel(index) {
    return 'item' + index + '-' + 'cost';
  }
  
  for (var d = 0; d &lt; data.length; d++) {
    var i = _.indexOf(uniqueDates, data[d].date);
    if (data[d].item !== undefined) {
      var item = data[d].item;
      if (values[item] === undefined) {
          values[item] = _.fill(Array(uniqueDates.length), null);
          values[item][0] = getLabel(item);
      }
      values[item][i] = data[d].cost;
      }
  }
  
  function checkIfUndefined(x) {
      return (x !== undefined);
  }
  
  function sparseToDense(data) {
    return data.filter(checkIfUndefined);
  }
  
  values = sparseToDense(values);
  </code></pre>