Reputation: 51
I have a csv file in the format:
"","Sequence","Paths","sequence_length"
"1","Social -> Social -> Social -> Social -> Social -> Social -> Social -> Social",29,8
"2","Social -> Social -> Social -> Social -> Social -> Social -> Social",30,7
"3","Social -> Social -> Social -> Social -> Social -> Social",40,6
"4","Social -> Social -> Social -> Social -> Social",71,5
"5","Social -> Social -> Social -> Social",156,4
"6","Social -> Social -> Social",273,3
"7","Social -> Social -> SEO",40,3
"8","Social -> Social",729,2
"9","Social -> SEO -> Social",51,3
"10","Social -> SEO",180,2
"11","Social -> SEM",56,2
I want to convert this into a JSON tree hierarchy as follows:
"Root" : [
{
"Sequence" : "Social",
"children" : [
{
"Sequence" : "Social",
"children" : [
{
"Sequence" : "Social",
"children" : [
{
"Sequence" : "Social",
"children" : [
{
"Sequence" : "Social",
"children" : [
{
"Sequence" : "Social",
"children" : [
{
"Sequence" : "Social",
"children" : [
{
"Sequence" : "Social",
"Path" : 29}],
}
}
Where Each Touch Point viz. 'Social' represented by -> in CSV file at each line represents the child of the previous and the Paths is added to the last node.
I am trying to split the Social things in one array as
data.forEach(function(d){
var x = d.Sequence.split(' -> ');
and then using the this x to parse into JSON.Could anyone please help me with it.Thanks !
Upvotes: 1
Views: 170
Reputation: 738
Your problem is that you already call your anonymous function masterTag
during the definition of the object. In this case, the function you assign to masterTag
is not a function of adserver
, it is a simple value assignment. And when that function is called during the object creation, it has not the right scope, i.e., this
is not the adserver
object.
E.g., this will work:
var adserver = {
createIframe : function (url) {
console.log(url);
},
trackSQ: (function () { })(),
masterTag: (function (type,cat) {
var self = this;
self.createIframe("test");
})
}
when you call
adserver.masterTag("targe0","maste0");
(I have changed your code a little bit to a minimal example)
Upvotes: 1