使用reduce对数据组中的数据组进行计数

时间:2019-06-03 19:01:36

标签: javascript

正如标题所述,我正在尝试对数据组中的数据组进行计数:我的逻辑是将所有数据分组为状态,然后将日期分组为年。这样输出应类似于:

state,year,count
mo,1910,2
in,1910,1
il,1910,3
or,1910,4
co,1910,2
nd,1910,1
...
mo,1911,5
in,1911,4
il,1911,6
or,1911,2
co,1911,8

我正在使用的CSV有很多需要的列,我只对列stateyear感兴趣。我下面的代码无法正常工作,任何帮助都将非常有用。

const countStates = filteredData.reduce((m, d) => {
    if(!m[d.year]){
      m[d.year] = {...d, count: 1};
      return m;
    }
    m[d.state];
    m[d.year];
    m[d.year].count += 1;
    return m;
},{});

const countedStates = Object.keys(countStates).map((k) =>  {
    const item  = countStates[k];
    return {
        state: item.state,
        year: item.year,
        count: item.count
    }
})

编辑

我使用的数据集示例:

datetime,city,state,country,shape,duration (seconds),duration (hours/min),comments,date posted,latitude,longitude 
10/10/1949 20:30,san marcos,tx,us,cylinder,2700,45 minutes,"This event took place in early fall around 1949-50. It occurred after a Boy Scout meeting in the Baptist Church. The Baptist Church sit",4/27/2004,29.8830556,-97.9411111
10/10/1949 21:00,lackland afb,tx,,light,7200,1-2 hrs,"1949 Lackland AFB&#44 TX.  Lights racing across the sky & making 90 degree turns on a dime.",12/16/2005,29.38421,-98.581082
10/10/1955 17:00,chester (uk/england),,gb,circle,20,20 seconds,"Green/Orange circular disc over Chester&#44 England",1/21/2008,53.2,-2.916667
10/10/1956 21:00,edna,tx,us,circle,20,1/2 hour,"My older brother and twin sister were leaving the only Edna theater at about 9 PM&#44...we had our bikes and I took a different route home",1/17/2004,28.9783333,-96.6458333
10/10/1960 20:00,kaneohe,hi,us,light,900,15 minutes,"AS a Marine 1st Lt. flying an FJ4B fighter/attack aircraft on a solo night exercise&#44 I was at 50&#44000&#39 in a "clean" aircraft (no ordinan",1/22/2004,21.4180556,-157.8036111
10/10/1961 19:00,bristol,tn,us,sphere,300,5 minutes,"My father is now 89 my brother 52 the girl with us now 51 myself 49 and the other fellow which worked with my father if he&#39s still livi",4/27/2007,36.5950000,-82.1888889
10/10/1965 21:00,penarth (uk/wales),,gb,circle,180,about 3 mins,"penarth uk  circle  3mins  stayed 30ft above me for 3 mins slowly moved of and then with the blink of the eye the speed was unreal",2/14/2006,51.434722,-3.18
10/10/1965 23:45,norwalk,ct,us,disk,1200,20 minutes,"A bright orange color changing to reddish color disk/saucer was observed hovering above power transmission lines.",10/2/1999,41.1175000,-73.4083333
10/10/1966 20:00,pell city,al,us,disk,180,3  minutes,"Strobe Lighted disk shape object observed close&#44 at low speeds&#44 and low altitude in Oct 1966 in Pell City Alabama",3/19/2009,33.5861111,-86.2861111
10/10/1966 21:00,live oak,fl,us,disk,120,several minutes,"Saucer zaps energy from powerline as my pregnant mother receives mental signals not to pass info",5/11/2005,30.2947222,-82.9841667
10/10/1968 13:00,hawthorne,ca,us,circle,300,5 min.,"ROUND &#44 ORANGE &#44 WITH WHAT I WOULD SAY WAS POLISHED METAL OF SOME KIND AROUND THE EDGES .",10/31/2003,33.9163889,-118.3516667
10/10/1968 19:00,brevard,nc,us,fireball,180,3 minutes,"silent red /orange mass of energy floated by three of us in western North Carolina in the 60s",6/12/2008,35.2333333,-82.7344444
10/10/1970 16:00,bellmore,ny,us,disk,1800,30 min.,"silver disc seen by family and neighbors",5/11/2000,40.6686111,-73.5275000

还有我用来过滤数据的代码:

fs.createReadStream('./data/scrubbed.csv')  
    .pipe(readCSV())
    .on('data', (data) => CSV.push(data))
    .on('end', () => {
        let data = CSV.map((d) => {
            return {
                year: new Date (d.datetime).getFullYear(),
                state: d.state,
                country: d.country
            }
    });

filteredData = data.filter((d) => {
    return d.state !== "" && d.country === "us"
})

哪个返回此值(数据集的一个示例):

[ { year: 1949, state: 'tx', country: 'us' },
  { year: 1956, state: 'tx', country: 'us' },
  { year: 1960, state: 'hi', country: 'us' },
  { year: 1961, state: 'tn', country: 'us' },
  { year: 1965, state: 'ct', country: 'us' }...

以上是我尝试使用reduce

分组的内容

1 个答案:

答案 0 :(得分:0)

下面的代码获取带有年份的状态数组,并使用reduce获取按年份分组的每个州的计数为

var data = [
    { year: '1910', state: 'mo', country: 'us' },
    { year: '1910', state: 'in', country: 'us' },
    { year: '1910', state: 'il', country: 'us' },
    { year: '1910', state: 'mo', country: 'us' },
    { year: '1911', state: 'in', country: 'us' },
    { year: '1911', state: 'il', country: 'us' },
    { year: '1911', state: 'mo', country: 'us' },
    { year: '1911', state: 'in', country: 'us' }
];

var organizedData = data.reduce((acc, val, index) => {
  acc[val.state] = acc[val.state] || {};
  acc[val.state][val.year] = (+acc[val.state][val.year] || 0) + 1;
  return acc;
}, {});

console.log(organizedData);