Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按天列出的功能数_Javascript_Json_D3.js - Fatal编程技术网

Javascript 按天列出的功能数

Javascript 按天列出的功能数,javascript,json,d3.js,Javascript,Json,D3.js,我有一个json文档,如下所示: { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "time": 1438342780, "title": "Iran's foreign minister calls for world's nuclear weapons states to disarm",

我有一个json文档,如下所示:

{
  "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
        ]
      }
    }
  ]
}
d3.json('path/to/json', function(json) {
    data = json;
});
我想获取json中的“feature”数组,并返回给定日期的功能数量。例如,对于上面的数据,我希望如下所示:

{
  "date": 7/31/2015,
  "number": 2
}
目前,我有一些东西看起来像这样:

{
  "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
        ]
      }
    }
  ]
}
d3.json('path/to/json', function(json) {
    data = json;
});

对于js和d3来说是相当新的,所以有点困惑。如果你需要更多的细节,请告诉我。提前谢谢

我不知道D3,但你可以用straight 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}
缺少的是将时间戳解析为日期

现在分组日期。也许现在下议院的选民可以撤销这一点了


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

这对您很有用,它返回一个对象数组。每个对象都是您要求的对象

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 } ]
var a=yourJSONObject,var map={},output=[];
对于(var i=0;i
这里最关键的一点是您的
时间
值是以历元时间为单位的,这意味着您必须使用将它们转换为预设日期

然后可以遍历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]++;
    }
}
var features=yourJSONObject.features;
var featuresByDate={};
对于(变量i=0,len=features.length;i
两个函数-一个用于获取数据,另一个用于遍历构建临时对象的功能,然后遍历对象以获得日期/数字对象数组

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
  }
]

这是您的日期字段吗<代码>“时间”:1438342780我想日期来自
web\u id
。是的,很抱歉,日期与
时间
键关联,并且是历元格式。是否
时间:1438342780
表示毫秒?不,仅限秒此示例仅适用于假设获取JSON的所有时间属性都与同一天相关的情况。因此注释“缺少的位正在将时间戳解析为日期”。抱歉,这几乎正是我想要的,除了日期解析返回1970年的所有日期,而根据数据,这些日期都在2015年。有什么想法吗?以我的示例中的日期解析为例。是的,我没有意识到您的时间引用是unix时间戳。乘以1000就行了。请参见上面的我的编辑@sammy8888888