2016-03-07 63 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 :) –