1

I have use d3.nest() for group by row values

Code is like this

function createNestingFunction(propertyName){
  return function(d){ 
        return d[propertyName];
     };
}

var levels = ['first', 'second', 'third']

var nest = d3.nest();
for (var i = 0; i < levels.length; i++) {
    nest = nest.key( createNestingFunction(levels[i]) );

}

var root = {
    "key":"root", 
    "values": nest.entries(data) //compute the nest
}

so result will be like this if there are two key ['locationId','userID']

[
 {
    "key":"12345646", <=== this group by first key locationID
    "values": [
      { 
        "key":"asdbnnmb12", <==== this group by second key userId
         "values":[]
      }
     ]
},
{
    "key":"12345we6", <=== this group by first key locationId
    "values": [
      { 
        "key":"hjhggg33", <==== this group by second key userId
         "values":[]
      }
     ]
}

]

now how can i identify then only with key ? key is value so what I expect is result like this

[
 {
    "key":"12345646", <=== this group by first key locationID
    "keyArrt":"locationID",
    "values": [
      { 
        "key":"asdbnnmb12", <==== this group by second key userId 
        "keyArrt":"userId",
         "values":[]
      }
     ]
}...

]

So i can identify the group by level because I have to redirect page based on grouped level and for that i dded to identify current level.

how can i add this key or any other attr with key and values so on groupby level i can identify them

More: example Just imaging you have social network data and you wanted to build hierarchy like this first level is location and than user and than their related post ( which is actually real row data )

So you will add two key for each level location and user .. it will work well but after getting data you are using this data somewhere in recursive or simple loop how will you identify that current node is user level goupr by or location it will only have key which is actually value not attr, key like location say USA,FRANCE than that level will be show key as USA and FRANCE, so same on user level group by key will be user name like AMIT or any other name .. so based on key we can't decide which group by is this .. so that what i wanted that if key is location value than just add one more attr like key attr and where we just show "location" so after that we can identify that this object have values group-by based on location or user..

2
  • 1
    You may be going about the problem the proper way, I'm not sure. I'm having a difficult time parsing what your initial data structure looks like and what your desired final data structure should look like. Could you put them into a plunker? Commented Jul 30, 2015 at 13:35
  • I have update question i can build plunker or fiddle but might be change in js that's why I didn't start it. Commented Jul 30, 2015 at 13:44

1 Answer 1

1

since .entries(data) returns an array you can use successive map methods to add keys attrs.

.map(function(d){
    var j = 0;
    d.keyAttr = levels[j];

    d.values.map(function(e){
     e.keyAttr = levels[++j];
     return e;
    });
    return d;
  });

Here is a plunker that shows this working with toy data.

Sign up to request clarification or add additional context in comments.

2 Comments

no what if i have 3 level for keys than i have to map that much level , plus if some of the group by don't have data than ?
for a third level, a third mapping function like e.values.map(function(f){ }) repeating that pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.