使用lowdb映射多个键 [英] Map multiple keys with lowdb
本文介绍了使用lowdb映射多个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Node.js的 lowdb 中,如果我有DB JSON之类的
In lowdb for Node.js, if I have a DB JSON like
[
{
"foo": "bar1",
"tar": "ball1",
"data": [
{
"a": 1
},
{
"b": 2
}
]
},
{
"foo": "bar2",
"tar": "ball2",
"data": [
{
"a": 1
},
{
"b": 2
}
]
}
]
我可以通过查询 db.get('test').map('foo').value();
获得所有foo: [bar1,bar2]
>
I can get all foo's: [bar1, bar2]
with the query db.get('test').map('foo').value();
但是我需要获取多个密钥,例如是否可以执行 db.get('test').map(['foo','data.a']).value();
可获得:
However I need to get several keys, like if I could do something like db.get('test').map(['foo', 'data.a']).value();
to get:
[
{
"foo": "bar1",
"data": [
{
"a": 1
}
]
},
{
"foo": "bar2",
"data": [
{
"a": 1
}
]
}
]
我该怎么做?
推荐答案
如果 data
是JSON,为什么不只使用 array.map()
?因此:
If data
is JSON, why not just use array.map()
? As such:
var data = db.get('test');
var dataSelection = data.map(e => {
return { foo: e.foo, a: data[0].a };
});
这篇关于使用lowdb映射多个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文