score:1

Accepted answer

Lots of ways to do this; here's a couple examples. The first uses d3.nest and turns the data into an array of arrays with the outer key being the name of the fruit. The second is a little hand-rolled code, which returns an object with properties of each fruit. What works best really depends on your larger use case.

<!DOCTYPE html>
<html>

  <head>
    <script data-require="d3@4.0.0" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script>
  </head>

  <body>
    <script>
    var data = [{
        "Fruits": 5,
        "Stores": 22,
        "Names of Fruits": "oranges"
      }, {
        "Fruits": 100,
        "Stores": 3,
        "Names of Fruits": "apple"
      },
      {
        "Fruits": 10,
        "Stores": 300,
        "Names of Fruits": "pear"
      },

      {
        "Fruits": 10,
        "Stores": 300,
        "Names of Fruits": "apple"
      }
    ];
    
    console.log(
      d3.nest()
        .key(function(d) { return d["Names of Fruits"]; })
        .entries(data)
    );
    
    var rv = {};
    data.forEach(function(d){
      if (!rv[d["Names of Fruits"]]) rv[d["Names of Fruits"]] = [];
      rv[d["Names of Fruits"]].push(d);
    });
    console.log(rv);
      
  </script>
  </body>

</html>


Related Query