基于内部数组值-D3跳过JSON数据记录

时间:2016-01-24 03:21:49

标签: javascript json d3.js visualization

我的数据如下。

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

我尝试过以下代码,但只出现了最后一个场地(场地2)。

代码:

                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>');

2 个答案:

答案 0 :(得分:1)

这是因为您在每次resultfilter来电时都会覆盖forEach。 在首次初始化resultfilter.push( <FILTER RESULT>)数组后,您可能希望在每个forEach循环中执行resultfilter之类的操作。像这样:

resultfilter = [];
data.forEach(function(a) {
    resultfilter.push(...);
}) 

(另外,看起来你复制的data对象有一些问题。如果它与复制/粘贴到这个问题无关,那么也要给它一个检查)

答案 1 :(得分:1)

可以使用Array.prototype.map()

简化此操作
 var  resultfilter = data.map(function(item) {
         item.values = item.values.filter(function(d) {
             return d.timestamp = 1449360000
         });
         return item;
 });