2017-04-26 88 views
1

我的格式爲csv文件:CSV成JSON轉換

"","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 

我打算將它轉換成JSON樹層次結構如下:

"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}], 
                  } 
                 } 

如果每個觸摸點即。在每行的CSV文件中用 - >表示的'Social'表示前一個的孩子,並且路徑被添加到最後一個節點。

我試圖在一個陣列中的社會分裂的事情作爲

data.forEach(function(d){ 
var x = d.Sequence.split(' -> '); 

,然後使用這張X解析成JSON.Could請人幫我it.Thanks!

+1

你如何調用該函數? – Guybrush

+0

當執行匿名函數時返回'undefined'。您可能想要在匿名函數中返回函數以在下次執行它。 –

+0

@Guybrush。 self.createIframe(URL); –

回答

1

你的問題是你已經在對象的定義過程中調用了你的匿名函數masterTag。在這種情況下,您分配給masterTag的功能不是adserver的功能,它是一種簡單的值分配。並且在創建對象期間調用該函數時,它的範圍不正確,即this不是adserver對象。

例如,這將工作:

var adserver = { 
    createIframe : function (url) { 
    console.log(url); 
    }, 
    trackSQ: (function() { })(), 
    masterTag: (function (type,cat) { 
    var self = this; 
    self.createIframe("test"); 
    }) 
} 

,當你調用

adserver.masterTag("targe0","maste0"); 

(我已經改變了你的代碼一點點的小例子)