score:0

this works good, but when combinations are missing, no entry is made, so it's not true wide data. i achieved it with this code:

// make data long
var flatdata = []
            distance_per_year.foreach(function(type) {
              type.values.foreach(function(typeyears) {
                flatdata.push({
                  type: type.key,
                  year: typeyears.key,
                  distance: typeyears.values,
                });
              });
            });

    // unique activity types
    groups = array.from(new set(flatdata.map(({ type }) => type)));
    years = array.from(new set(flatdata.map(({ year }) => year)));

    // make wide data with 0 where value is missing
    var grouped = years.map(function(d) {
      var item = {
        year:  d
      };
      groups.foreach(function(e) {
        var itemforgroup = flatdata.filter(function(f) {
          return f.type === e && f.year === d;
        });
        if (itemforgroup.length) {
          item[e] = number(itemforgroup[0].distance);
        } else {
          item[e] = 0;
        }
      })
      return item;
    })

this produces first an unnested long dataset first (3 columns), then reshapes it to wide and fills in 0s where distance is missing.

the source are these two questions: reshape data for d3 stacked bar chart and d3: flatten nested data?

score:1

based on @unminder's answer but just to get every property into the object:

stakblitz example: https://stackblitz.com/edit/js-d5sitf

const entryarr = [
  {
    key: 'ride',
    values: [
      { key: '2008', value: 3234 },
      { key: '2009', value: 3234 },
      { key: '2010', value: 5000 },
      { key: '2011', value: 6000 },
      { key: '2012', value: 1456 },
      { key: '2013', value: 2453 }
    ]
  },
  {
    key: 'hike',
    values: [
      { key: '2006', value: 3234 },
      { key: '2009', value: 2420 },
      { key: '2010', value: 4530 },
      { key: '2011', value: 2000 },
      { key: '2012', value: 1900 },
      { key: '2013', value: 3700 }
    ]
  },
  {
    key: 'walk',
    values: [
      { key: '2009', value: 3001 },
      { key: '2010', value: 1090 },
      { key: '2011', value: 2020 },
      { key: '2012', value: 2000 },
      { key: '2013', value: 6000 }
    ]
  }
];

const getkeys = entryarr.map(x => x.key);
const result = [];
entryarr.foreach(a => a.values.foreach(v => {
  const element = result.find(e => e.year === v.key);
  if (element) {
    element[a.key] = v.value;
  } else {
    const obj = getkeys.reduce((acc, x) => ({ ...acc, year: v.key, [x]: a.key === x ? v.value : '' })
      , {});
    result.push(obj);
  }
}));

console.log(result)

note: it can be improved using reducers to avoid global variables

score:2

this could be done as follows:

const data = [];
mydata.foreach(a => a.values.foreach(v => {
    const element = data.find(e => e.year === v.key);
    if (element) {
        element[a.key] = v.values;
    } else {
        data.push({ year: v.key, [a.key]: v.values });
    }
}));

Related Query

More Query from same tag