[英]LowDB get object with highest timestamp
"log": [
{
"id": "H1AXR_ns7",
"batt": 80,
"lat": "lat here",
"lon": "lon here",
"tst": 1540552932
},
{
"id": "H1AXR_ns7",
"batt": 80,
"lat": "lat here",
"lon": "lon here",
"tst": 1540531234
},
{
"id": "H1AXR_ns7",
"batt": 80,
"lat": "lat here",
"lon": "lon here",
"tst": 1540511234
}
]
In LowDB, I would like to filter and get the object with the highest tst
value for the user with id: H1AXR_ns7
. 在LowDB中,我想为ID为
H1AXR_ns7
的用户过滤并获取具有最高tst
值的对象。
I'm not sure how to do this: 我不确定如何执行此操作:
I have the following code so far: 到目前为止,我有以下代码:
db.get('log')
.find(id)
.filter().value()
I know that I have to write an expression in the .filter()
, but I have no idea how to write it to get the higest tst
for user with id H1AXR_ns7
. 我知道我必须在
.filter()
编写一个表达式,但是我不知道如何编写该表达式以获取ID为H1AXR_ns7
用户的最大tst
H1AXR_ns7
。
let [highest] = log.sort((a,b)=>a.tst - b.tst)
// highest holds that object now
const id = 'H1AXR_ns7';
const getMax = (accumulator, currentValue) => accumulator.tst < currentValue.tst? currentValue: accumulator;
const latest = log
.filter(p => p.id === id)
.reduce(getMax);
Documentation for filter: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 过滤器文档: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
Documentation for reduce: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce 减少文档: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.