1

Ok, after a couple of hours of googling I haven't found a solution that (works) and doesn't use eval(). I have an array which i would like to run the same function to nest and do roll-up sums.

dsAhsBes = createAhsBesok(dsFilterBes, dsAhsBes, "d.Ar")

function createAhsBesok(ds, output, groupVar) {

  output = d3.nest()
    .key(function(d) {
      return eval(groupVar);
    })
    .rollup(function(d) {
      return {
        "Count": d3.sum(d, function(g) {
          return g.Count;
        })
      };
    }).entries(ds);

  output.forEach(function(d) {
    d.Count = +d.values.Count;
    d.Dep = "Main";
  });

  return (output);
}

The snippet above works, but I rather use something else than eval(), if possible. How do i get the child function to parse groupVar the right way without using eval?

Tushar
  • 85,780
  • 21
  • 159
  • 179
ErrantBard
  • 1,421
  • 1
  • 21
  • 40
  • 2
    Don't pass any argument, it'll be `undefined` – Tushar Jul 16 '15 at 09:25
  • Well, but I still would like to specify which key (or different values for groupvar) to do roll-up sums. I plan to use this function for other elements besides "Ar". – ErrantBard Jul 16 '15 at 09:27
  • 2
    Maybe just use square brackets? `function foobar(key){ return d[key]; }; foobar('Ar');` – EJTH Jul 16 '15 at 09:28
  • @EJTH Yeah - of course. That was stupid of me - that was exactly what I wanted to do and which I should have known. Thanks! :) – ErrantBard Jul 16 '15 at 09:32
  • 1
    @ErrantBard, also if you have case when you need not first property like: `"d.Ar"`, but something like `"d.Ar.Ar2.Ar2"` you can see this [question](http://stackoverflow.com/questions/25306541/dynamically-access-object-propertys-js/25306848) – Grundy Jul 16 '15 at 09:49

0 Answers0