我的数据如下。
data=[ {"venue":"Foyer","values":[{"avg":996.8,"sessions":[{"minTime":0,"maxTime":599,"count":50},
{"minTime":600,"maxTime":1199,"count":39},
{"minTime":1800,"maxTime":2399,"count":43},
{"minTime":2400,"maxTime":2999,"count":48}],"timestamp":1449360000},
{"avg":986.4,"sessions":[{"minTime":0,"maxTime":599,"count":152},
{"minTime":600,"maxTime":1199,"count":51},
{"minTime":1200,"maxTime":1799,"count":36},
{"minTime":1800,"maxTime":2399,"count":68},
{"minTime":3000,"maxTime":3599,"count":34}],"timestamp":1452540000}]},
{"venue":"Venue1","values":[{"avg":996.8,"sessions":[{"minTime":0,"maxTime":599,"count":62},
{"minTime":600,"maxTime":1199,"count":33},
{"minTime":1800,"maxTime":2399,"count":37},
{"minTime":2400,"maxTime":2999,"count":24}],"timestamp":1449360000},
{"avg":986.4,"sessions":[{"minTime":0,"maxTime":599,"count":34},
{"minTime":600,"maxTime":1199,"count":52},
{"minTime":1200,"maxTime":1799,"count":21},
{"minTime":1800,"maxTime":2399,"count":142},
{"minTime":3000,"maxTime":3599,"count":23}],"timestamp":1452540000}]},
{"venue":"Venue2","values":[{"avg":996.8,"sessions":[{"minTime":0,"maxTime":599,"count":44},
{"minTime":600,"maxTime":1199,"count":25},
{"minTime":1800,"maxTime":2399,"count":34},
{"minTime":2400,"maxTime":2999,"count":23}],"timestamp":1449360000},
{"avg":986.4,"sessions":[{"minTime":0,"maxTime":599,"count":31},
{"minTime":600,"maxTime":1199,"count":11},
{"minTime":1200,"maxTime":1799,"count":122},
{"minTime":1800,"maxTime":2399,"count":12},
{"minTime":3000,"maxTime":3599,"count": 3}],"timestamp":1452540000}]}]
我只想从具有匹配时间戳的“值”中过滤记录。对于不匹配的时间戳记,“值”中的条目应进行过滤,但输出应具有相同的数据结构,
预期产量:
[{"venue":"Foyer","values":[{"avg":996.8,"sessions":[{"minTime":0,"maxTime":599,"count":50},
{"minTime":600,"maxTime":1199,"count":39},
{"minTime":1800,"maxTime":2399,"count":43},
{"minTime":2400,"maxTime":2999,"count":48}],"timestamp":1449360000}]},
{"venue":"Venue1","values":[{"avg":996.8,"sessions":[{"minTime":0,"maxTime":599,"count":62},
{"minTime":600,"maxTime":1199,"count":33},
{"minTime":1800,"maxTime":2399,"count":37},
{"minTime":2400,"maxTime":2999,"count":24}],"timestamp":1449360000}]},
{"venue":"Venue2","values":[{"avg":996.8,"sessions":[{"minTime":0,"maxTime":599,"count":44},
{"minTime":600,"maxTime":1199,"count":25},
{"minTime":1800,"maxTime":2399,"count":34},
{"minTime":2400,"maxTime":2999,"count":23}],"timestamp":1449360000}]}]
我尝试使用以下代码,但是仅显示最后一个场所(venue2)。
码:
d3.json("Dwell.json", function(error,data){
data.forEach(function(a) {
resultfilter={ "venue": a.venue, "values":a.values.filter(function (d) { return d.timestamp = 1449360000 } ) }
})
document.write('<pre>' + JSON.stringify(resultfilter) + '</pre>');
最佳答案
这是因为您在每次resultfilter
调用时都会覆盖forEach
。
在第一次初始化resultfilter.push( <FILTER RESULT>)
数组之后,您可能想在每个forEach
循环中执行类似resultfilter
的操作。像这样:
resultfilter = [];
data.forEach(function(a) {
resultfilter.push(...);
})
(另外,看起来您复制的
data
对象有一些问题。如果它与复制/粘贴到此问题无关,那么也请进行检查)