2016-03-07 67 views
1

這個想法是將所有「位置」合併在一起,爲我的圖表提供一個數字供我閱讀。所以可以說你有5個「位置」,紐約有不同的「日期」。我想將所有5個合併在一起,並輸出5個數字,以及.json中合併的「Punches」。現在我有它抓住地點,併合並類似拳。但我想獲得紐約實例的總數,並輸出該數字。組合位置並輸出來自JSON的組合數據

chart.json

[ 
    { 
     "Date":"2003", 
     "Punches":"0", 
     "Locations":"New York" 
    }, 
    { 
     "Date":"2003", 
     "Punches":"1", 
     "Locations":"New York" 
    }, 
    { 
     "Date":"2004", 
     "Punches":"0", 
     "Locations":"Chicago" 
    }, 
    { 
     "Date":"2004", 
     "Punches":"1", 
     "Locations":"Chicago" 
    }, 
    { 
     "Date":"2004", 
     "Punches":"1", 
     "Locations":"Ohio" 
    }, 
    { 
     "Date":"2004", 
     "Punches":"1", 
     "Locations":"Ohio" 
    }, 
    { 
     "Date":"2007", 
     "Punches":"0", 
     "Locations":"Ohio" 
    }, 
    { 
     "Date":"2007", 
     "Punches":"0", 
     "Locations":"Florida" 
    }, 
    { 
     "Date":"2009", 
     "Punches":"1", 
     "Locations":"Florida" 
    }, 
    { 
     "Date":"2007", 
     "Punches":"0", 
     "Locations":"New York" 
    }, 
    { 
     "Date":"2009", 
     "Punches":"0", 
     "Locations":"New York" 
    }, 
    { 
     "Date":"2009", 
     "Punches":"0", 
     "Locations":"Chicago" 
    }, 
    { 
     "Date":"2010", 
     "Punches":"0", 
     "Locations":"New York" 
    }, 
    { 
     "Date":"2010", 
     "Punches":"0", 
     "Locations":"Florida" 
    } 
] 

JS

function LocationMerge() 
     { 
      $.ajax(
      { 
       url: 'data.json', 
       data:{}, 
       dataType: 'json', 
       success: function(data) 
       { 
        var string = JSON.stringify(data); 
        var objects = $.parseJSON(string); 
        var categories = new Array(); 
        var mergedPieces = new Array(); 
        var i = 0; 
        _.each(objects, function(obj) 
        { 
         var existingObj; 
         if ($.inArray(obj.Locations, categories) >= 0) 
         { 
          existingObj = _.find(objects, function(o) 
          { 
           return o.Locations=== obj.Locations; 
          }); 
          existingObj["Punches"] += obj["Punches"]; 
         } 
         else 
         { 
          mergedPieces[i] = obj; 
          categories[i] = obj.Locations; 
          i++; 
         } 
        }); 
        mergedPieces = _.sortBy(mergedPieces, function(obj) 
        { 
         return obj["Punches"]; 
        }).reverse(); 
        _.each(mergedPieces, function(obj) 
        { 
         var output = ''; 
         _.each(obj, function(val, key) 
         { 
          output += key + ': ' + val + '<br>'; 
         }); 
         output += '<br>'; 
         console.log(output); 
        }); 
       } 
      }); 
     } 

回答

0

試試這個在你的成功的功能。檢查出的演示(控制檯輸出)的結果

var data = [{"Date": "2003", "Punches": "0", "Locations": "New York"}, {"Date": "2003", "Punches": "1", "Locations": "New York"}, {"Date": "2004", "Punches": "0", "Locations": "Chicago"}, {"Date": "2004", "Punches": "1", "Locations": "Chicago"}, {"Date": "2004", "Punches": "1", "Locations": "Ohio"}, {"Date": "2004", "Punches": "1", "Locations": "Ohio"}, {"Date": "2007", "Punches": "0", "Locations": "Ohio"}, {"Date": "2007", "Punches": "0", "Locations": "Florida"}, {"Date": "2009", "Punches": "1", "Locations": "Florida"}, {"Date": "2007", "Punches": "0", "Locations": "New York"}, {"Date": "2009", "Punches": "0", "Locations": "New York"}, {"Date": "2009", "Punches": "0", "Locations": "Chicago"}, {"Date": "2010", "Punches": "0", "Locations": "New York"}, {"Date": "2010", "Punches": "0", "Locations": "Florida"}]; 

function LocationMerge() 
{ 
    var newObj = new Object(); 
    _.each(data, function(obj){ 
     if(newObj[obj.Locations] === undefined) 
      newObj[obj.Locations] = {"Location":obj.Locations,"Punches":parseInt(obj.Punches),"items":1}; 
     else{ 
      newObj[obj.Locations]["Punches"]+=parseInt(obj.Punches); 
      newObj[obj.Locations]["items"]++; 
     } 
    }); 
    console.log(newObj); 
} 

LocationMerge(); 

DEMO

+0

謝謝!這是太棒了。 – askmeaquestion1234

+0

@ askmeaquestion1234:請關閉解決方案,如果它適合你 –

1
Code below 

var dataset = [ 
 
    { 
 
     "Date":"2003", 
 
     "Punches":"0", 
 
     "Locations":"New York" 
 
    }, 
 
    { 
 
     "Date":"2003", 
 
     "Punches":"1", 
 
     "Locations":"New York" 
 
    }, 
 
    { 
 
     "Date":"2004", 
 
     "Punches":"0", 
 
     "Locations":"Chicago" 
 
    }, 
 
    { 
 
     "Date":"2004", 
 
     "Punches":"1", 
 
     "Locations":"Chicago" 
 
    }, 
 
    { 
 
     "Date":"2004", 
 
     "Punches":"1", 
 
     "Locations":"Ohio" 
 
    }, 
 
    { 
 
     "Date":"2004", 
 
     "Punches":"1", 
 
     "Locations":"Ohio" 
 
    }, 
 
    { 
 
     "Date":"2007", 
 
     "Punches":"0", 
 
     "Locations":"Ohio" 
 
    }, 
 
    { 
 
     "Date":"2007", 
 
     "Punches":"0", 
 
     "Locations":"Florida" 
 
    }, 
 
    { 
 
     "Date":"2009", 
 
     "Punches":"1", 
 
     "Locations":"Florida" 
 
    }, 
 
    { 
 
     "Date":"2007", 
 
     "Punches":"0", 
 
     "Locations":"New York" 
 
    }, 
 
    { 
 
     "Date":"2009", 
 
     "Punches":"0", 
 
     "Locations":"New York" 
 
    }, 
 
    { 
 
     "Date":"2009", 
 
     "Punches":"0", 
 
     "Locations":"Chicago" 
 
    }, 
 
    { 
 
     "Date":"2010", 
 
     "Punches":"0", 
 
     "Locations":"New York" 
 
    }, 
 
    { 
 
     "Date":"2010", 
 
     "Punches":"0", 
 
     "Locations":"Florida" 
 
    } 
 
]; 
 

 
map = {}; 
 
dataset.forEach(function(data){ if(map[data.Locations]){map[data.Locations]=map[data.Locations]+1}else{map[data.Locations]=1}}); 
 

 
snippet.log(map)
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

+1

1 up for a line line solution :) –