2016-02-25 40 views
1

我有一个JSON文档看起来像这样:数日间功能的JavaScript

{ 
    "type": "FeatureCollection", 
    "features": [ 
    { 
     "type": "Feature", 
     "properties": { 
     "time": 1438342780, 
     "title": "Iran's foreign minister calls for world's nuclear weapons states to disarm", 
     "author": "Julian Borger", 
     "web_id": "world/2015/jul/31/iran-nuclear-weapons-states-disarm-israel" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      -77.26526, 
      38.90122 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": 1438300867, 
     "title": "Big bangs over the white cliffs of Dover as unique 1915 artillery gun is fired again", 
     "author": "Maev Kennedy", 
     "web_id": "world/2015/jul/31/big-bangs-over-white-cliffs-dover-unique-1915-artillery-gun-fired-again" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      1.3, 
      51.13333 
     ] 
     } 
    } 
    ] 
} 

我想取里面的JSON“功能”数组并返回了许多功能对于一个给定的一天。例如,对于上述数据我希望是这样的:

{ 
    "date": 7/31/2015, 
    "number": 2 
} 

目前,我有一些看起来像这样:

d3.json('path/to/json', function(json) { 
    data = json; 
}); 

相当新的JS和D3所以有点难倒。让我知道你是否需要更多细节。提前致谢!

+1

这是您的日期字段? ''时间“:1438342780' –

+0

我认为日期来自'web_id'。 – Andy

+0

是的,对不起,日期与'时间'键相关并且处于时代格式。 – sammy88888888

回答

2

这会为你工作,它返回一个对象数组。每个对象都是你问的对象。

var a = yourJSONObject, var map = {}, output = []; 
for (var i = 0; i < a.features.length; i++) { 
var ref = new Date(a.features[i].properties.time*1000).toDateString(); 
if (map[ref] == undefined) { 
    map[ref] = output.push({ 
    date: ref, 
    number: 1 
    }) - 1; 
} else 
    output[map[ref]].number++ 
} 

console.log(output) //[ { date: 'Sat Jan 17 1970', number: 2 } ] 
+0

这几乎是我正在寻找的东西,除了根据http://www.epochconverter.com在1970年日期解析返回所有日期。有什么想法吗? – sammy88888888

+0

从我的例子中解析日期。 –

+1

是的,我没有意识到你的时间参考是一个unix时间戳。 只需乘以1000即可。看看我上面的编辑@ sammy88888888 – Morrisda

-1

我不知道D3,但你可以直JS做到这一点:

var json = { 
    "features": [{ 
     "type": "Feature", 
     "properties": { 
      "time": 1438342780, 
      "title": "Iran's foreign minister calls for world's nuclear weapons states to disarm" 
     } 
    }, { 
     "type": "Feature", 
     "properties": { 
      "time": 1438300867, 
      "title": "Big bangs over the white cliffs of Dover as unique 1915 artillery gun is fired again" 
     } 
    }, { 
     "type": "Feature same date", 
     "properties": { 
      "time": 1448300867, 
      "title": "Big bangs over the white cliffs of Dover as unique 1915 artillery gun is fired again" 
     } 
    }] 
} 

var counts = {} 

function secondsToDate(seconds) { 
    var date = new Date(1970,0,1); 
    date.setSeconds(seconds); 
    return date.toDateString(); 
} 

json.features.reduce((counts, feature) => { 
    var date = secondsToDate(feature.properties.time) 
    if (counts[date]) { 
     counts[date]++ 
    } else { 
     counts[date] = 1 
    } 
    return counts 
}, counts) 

console.log(counts) // {'Fri Jul 31 2015': 2, 'Mon Nov 23 2015': 1} 

的缺失位解析您的时间戳为日期。

现在按日期分组。也许现在downvoter可以撤销!

我添加了一个带有复制时间戳的对象来突出显示计数正在增加。

+0

此示例仅适用于假设获取JSON的所有时间属性均与当天相关的情况。 – Morrisda

+0

因此评论“缺少的位是解析您的时间戳到日期。” –

+0

对不起,你的意思是这个 – Morrisda

1

这里的关键部分是,你time值是划时代的时间,这意味着你必须将它们转换使用this technique预设日期。

然后,您可以遍历features数组,并跟踪每个日期的计数。

var features = yourJSONObject.features; 
var featuresByDate = {}; 

for (var i = 0, len = features.length; i < len; i++) { 
    // find the current feature's date 
    var epochTime = features[0].properties.time; 
    var date = new Date(0); 
    date.setUTCSeconds(epochTime); 

    // find the date in 7/31/2015 format 
    var dateStr = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear(); 

    // count the date for the first time if it has not been counted yet 
    if (! featuresByDate.hasOwnProperty(dateStr)) { 
     featuresByDate[dateStr] = 1; 
    } 
    // otherwise, increment its counter 
    else { 
     featuresByDate[dateStr]++; 
    } 
} 
0

两个函数 - 一个拿到correct date based on the epoch time,另一方面通过建立一个临时的对象,然后通过对象迭代给你日期/数字对象的数组功能进行迭代。

function getDate(time) { 
    var d = new Date(0); 
    d.setUTCSeconds(time); 
    return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/'); 
} 

function getData(data) { 
    var obj = data.features.reduce(function(p, c) { 
    var date = getDate(c.properties.time); 
    p[date] = (p[date] + 1) || 1; 
    return p; 
    }, {}); 
    return Object.keys(obj).map(function (el) { 
    return { date: el, number: obj[el] }; 
    }); 
} 

getData(data); 

输出

[ 
    { 
    "date": "7/31/2015", 
    "number": 2 
    } 
] 

DEMO