Convert flat array of objects into nested array of objects

前端 未结 3 1618
一生所求
一生所求 2021-02-04 07:00

Original JSON data (flat table):

[
    {\"id\":\"1\",\"first_name\":\"Jason\",\"last_name\":\"Martin\",\"start_date\":\"1996-07-25\",\"end_date\":\"2006-07-25\",         


        
3条回答
  •  深忆病人
    2021-02-04 07:41

    Building on the example provided by @nrabinowitz, here's the nest function with the originally proposed API of passing the collection and an array of property names as args, using d3.nest under the hood:

    function nest(data, keys) {
      var nest = d3.nest();
      keys.forEach(function(k) { 
        nest.key(function(d) {
          return d[k];
        })
      });
      return nest.entries(data);
    }
    

提交回复
热议问题