user2148971 user2148971 - 15 days ago 6
Node.js Question

lodash _.filter timestamp between range of timestamp?

Here my data:

[
{
"idi": 0,
"ut": 1479679625,
"eid": 0,
"ide": 0
},
{
"idi": 1,
"ut": 1479679630,
"eid": 1,
"ide": 0
},
{
"idi": 2,
"ut": 1479679650,
"eid": 0,
"ide": 1
},
{
"idi": 3,
"ut": 1479679655,
"eid": 1,
"ide": 1
}
]


ut
is unix timestamp, I need to get all objects between range of 2 ut values. like >= 1479679625 && <= 1479679655 (all objects between first & last object). I use lodash with lowdb and nodejs:

const low = require('lowdb');
var _ = require('lodash');

var DB = low('pathDB');
var TIME_QUERY_START_UNIX = 1479679625;
var TIME_QUERY_STOP_UNIX = 1479679655;
var QUERY = _.filter(DB, function(o) { return TIME_QUERY_START_UNIX <= o.ut && o.ut <= TIME_QUERY_STOP_UNIX; });


but it throws an


Cannot read property 'ut' of undefined


How I do a right command?

Thanks

Answer
_.filter(data, function(item) {
    return _.inRange(
        item.ut, 
        TIME_QUERY_START_UNIX, 
        TIME_QUERY_STOP_UNIX + 1
    );
})

check item.eid = 0

_.filter(data, function(item) {
    return _.every([
        _.inRange(
            item.ut, 
            TIME_QUERY_START_UNIX, 
            TIME_QUERY_STOP_UNIX + 1
        ),
        item.eid === 0
    ]);
})