簡體   English   中英

通過比較兩個鍵來連接兩個json數組

[英]join two json arrays by comparing two keys

我不想通過比較每個數組中的兩個字段來連接兩個json數組(例如SQL join:請參見代碼中的if),並將第二個數組中的值添加到第一個數組中。 我嘗試通過使用嵌套循環來解決此問題,並將值從內部循環傳遞到外部循環(請參見代碼)。 但是結果始終為0。

曼尼謝謝!

這是代碼示例:

var dmNodeGraph = [ { id: 'marathonschaatsteams',
        group: '9',
        type: 'child',
        color: '33cccc' },
      { id: 'KNSB', group: '9', type: 'child', color: '33cccc' },
      { id: 'stationshuisje',
        group: '9',
        type: 'child',
        color: '33cccc' },
      { id: 'RTV', group: '9', type: 'child', color: '33cccc' },
      { id: 'wachten', group: '5', type: 'child', color: '99cc00' },
      { id: 'smartcards', group: '9', type: 'child', color: '33cccc' } ]

 var groupByLink =  [ { key: 'stationshuisje-kpn-9', values: 1 },
      { key: 'RTV-tele2-9', values: 14 },
      { key: 'Spelen-kpn-9', values: 13 },
      { key: 'hoofdsponsor-kpn-9', values: 14 },
      { key: 'schaatsen-kpn-9', values: 13 },
      { key: 'KNSB-kpn-9', values: 17 },
      { key: 'klanten-ziggo-9', values: 3 },
      { key: 'kaarten-ziggo-9', values: 1 },
      { key: 'wachten-ziggo-9', values: 1 },
      { key: 'marathonschaatsteams-kpn-9', values: 1 },
      { key: 'wachten-tele2-9', values: 1 },
      { key: 'gebruiken-kpn-9', values: 1 },
      { key: 'wachten-kpn-9', values: 1 },
      { key: 'abonnementen-kpn-9', values: 2 },
      { key: 'wachten-Glasvezel-5', values: 1 },
      { key: 'smartcards-ziggo-9', values: 2 } ]



 $('#test').append('This is from the loop <br>')
 for (var i = 0; i < dmNodeGraph.length; i++){
       var id  = dmNodeGraph[i].id
       var group = dmNodeGraph[i].group
       var value
            //$('#test').append('<br> ------------------- id: ' + id + ' group: ' + group + '--------------------'  )
       // 4.A.1.A Loop per Node through the grouped links
       for (var gl = 0 ; gl < groupByLink.length ; gl++){
         //value = 0
         var keys = groupByLink[gl].key.split('-')
         if (keys[0] == id && keys[2] == group){
           value = groupByLink[gl].values 

           // Location A 
           //$('#test').append('<br> id: ' + id + ' group: ' + group + ' values: ' + value ) 
         }
        }
                // Location B
          dmNodeGraph[i].values = value
          $('#test').append('<br> id: ' + id + ' group: ' + group + ' values: ' + value ) 
         }

該代碼示例也可以在jsFidle( https://jsfiddle.net/L67nwzs0/22/ )中找到

刪除賦值value=0 .//覆蓋該值。

 var dmNodeGraph = [ { id: 'marathonschaatsteams', group: '9', type: 'child', color: '33cccc' }, { id: 'KNSB', group: '9', type: 'child', color: '33cccc' }, { id: 'stationshuisje', group: '9', type: 'child', color: '33cccc' }, { id: 'RTV', group: '9', type: 'child', color: '33cccc' }, { id: 'wachten', group: '5', type: 'child', color: '99cc00' }, { id: 'smartcards', group: '9', type: 'child', color: '33cccc' } ] var groupByLink = [ { key: 'stationshuisje-kpn-9', values: 1 }, { key: 'RTV-tele2-9', values: 14 }, { key: 'Spelen-kpn-9', values: 13 }, { key: 'hoofdsponsor-kpn-9', values: 14 }, { key: 'schaatsen-kpn-9', values: 13 }, { key: 'KNSB-kpn-9', values: 17 }, { key: 'klanten-ziggo-9', values: 3 }, { key: 'kaarten-ziggo-9', values: 1 }, { key: 'wachten-ziggo-9', values: 1 }, { key: 'marathonschaatsteams-kpn-9', values: 1 }, { key: 'wachten-tele2-9', values: 1 }, { key: 'gebruiken-kpn-9', values: 1 }, { key: 'wachten-kpn-9', values: 1 }, { key: 'abonnementen-kpn-9', values: 2 }, { key: 'wachten-Glasvezel-5', values: 1 }, { key: 'smartcards-ziggo-9', values: 2 } ] $('#test').append('This is from the loop <br>') for (var i = 0; i < dmNodeGraph.length; i++){ var id = dmNodeGraph[i].id var group = dmNodeGraph[i].group var value=0;//declare here //$('#test').append('<br> ------------------- id: ' + id + ' group: ' + group + '--------------------' ) // 4.A.1.A Loop per Node through the grouped links for (var gl = 0 ; gl < groupByLink.length ; gl++){ var keys = groupByLink[gl].key.split('-'); if (keys[0] == id && keys[2] == group){ value = groupByLink[gl].values // Location A //$('#test').append('<br> id: ' + id + ' group: ' + group + ' values: ' + value ) } } // Location B dmNodeGraph[i].values = value $('#test').append('<br> id: ' + id + ' group: ' + group + ' values: ' + value ); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='test'> </div> 

如果您不想使用第三個變量,請嘗試以下操作

dmNodeGraph.push.apply( dmNodeGraph, groupByLink );



console.log(dmNodeGraph) // contains both dmNodeGraph and groupByLink

您每次迭代都會將值初始化為0,並且最后一次聚合會丟失

 var dmNodeGraph = [ { id: 'marathonschaatsteams', group: '9', type: 'child', color: '33cccc' }, { id: 'KNSB', group: '9', type: 'child', color: '33cccc' }, { id: 'stationshuisje', group: '9', type: 'child', color: '33cccc' }, { id: 'RTV', group: '9', type: 'child', color: '33cccc' }, { id: 'wachten', group: '5', type: 'child', color: '99cc00' }, { id: 'smartcards', group: '9', type: 'child', color: '33cccc' } ] var groupByLink = [ { key: 'stationshuisje-kpn-9', values: 1 }, { key: 'RTV-tele2-9', values: 14 }, { key: 'Spelen-kpn-9', values: 13 }, { key: 'hoofdsponsor-kpn-9', values: 14 }, { key: 'schaatsen-kpn-9', values: 13 }, { key: 'KNSB-kpn-9', values: 17 }, { key: 'klanten-ziggo-9', values: 3 }, { key: 'kaarten-ziggo-9', values: 1 }, { key: 'wachten-ziggo-9', values: 1 }, { key: 'marathonschaatsteams-kpn-9', values: 1 }, { key: 'wachten-tele2-9', values: 1 }, { key: 'gebruiken-kpn-9', values: 1 }, { key: 'wachten-kpn-9', values: 1 }, { key: 'abonnementen-kpn-9', values: 2 }, { key: 'wachten-Glasvezel-5', values: 1 }, { key: 'smartcards-ziggo-9', values: 2 } ] $('#test').append('This is from the loop <br>') for (var i = 0; i < dmNodeGraph.length; i++){ var id = dmNodeGraph[i].id var group = dmNodeGraph[i].group var value=0; // initiallize with 0 for ever new iteration for (var gl = 0 ; gl < groupByLink.length ; gl++){ // value = 0 <-------- remove this one var keys = groupByLink[gl].key.split('-') if (keys[0] == id && keys[2] == group){ value += groupByLink[gl].values } } dmNodeGraph[i].values = value $('#test').append('<br> id: ' + id + ' group: ' + group + ' values: ' + value ) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='test'> </div> 

這是jsfiddle的工作版本。 我只是簡單地取出了value變量,並直接在內循環內部使用了dmNodeGraph[i].values = groupByLink[gl].values

刪除對value多余分配

 var dmNodeGraph = [ { id: 'marathonschaatsteams', group: '9', type: 'child', color: '33cccc' }, { id: 'KNSB', group: '9', type: 'child', color: '33cccc' }, { id: 'stationshuisje', group: '9', type: 'child', color: '33cccc' }, { id: 'RTV', group: '9', type: 'child', color: '33cccc' }, { id: 'wachten', group: '5', type: 'child', color: '99cc00' }, { id: 'smartcards', group: '9', type: 'child', color: '33cccc' } ] var groupByLink = [ { key: 'stationshuisje-kpn-9', values: 1 }, { key: 'RTV-tele2-9', values: 14 }, { key: 'Spelen-kpn-9', values: 13 }, { key: 'hoofdsponsor-kpn-9', values: 14 }, { key: 'schaatsen-kpn-9', values: 13 }, { key: 'KNSB-kpn-9', values: 17 }, { key: 'klanten-ziggo-9', values: 3 }, { key: 'kaarten-ziggo-9', values: 1 }, { key: 'wachten-ziggo-9', values: 1 }, { key: 'marathonschaatsteams-kpn-9', values: 1 }, { key: 'wachten-tele2-9', values: 1 }, { key: 'gebruiken-kpn-9', values: 1 }, { key: 'wachten-kpn-9', values: 1 }, { key: 'abonnementen-kpn-9', values: 2 }, { key: 'wachten-Glasvezel-5', values: 1 }, { key: 'smartcards-ziggo-9', values: 2 } ] for (var i = 0; i < dmNodeGraph.length; i++) { var id = dmNodeGraph[i].id var group = dmNodeGraph[i].group for (var gl = 0; gl < groupByLink.length; gl++) { var keys = groupByLink[gl].key.split('-') if (keys[0] == id && keys[2] == group) { var value = groupByLink[gl].values console.log(id + ', ' + group + ', ' + value) } } } 

groupByLink.map((e)=>dmNodeGraph.push(e))
console.log(dmNodeGraph);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM