我正在使用lowdb从列表中弹出对象
{
"posts": [
{ "id": a, "title": "lowdb is awesome"},
{ "id": b, "title": "lowdb is awesome"},
{ "id": c, "title": "lowdb is awesome"}
],
"user": {
"name": "typicode"
},
"count": 3
}
并且需要弄清楚如何从帖子中“弹出”第一个插入的对象:
db.get('posts')
.find()
.value()
我希望返回{ "id": a, "title": "lowdb is awesome"}
,并且帖子将反映此内容
删除数组中的第一项只需使用shift()
方法即可完成。请注意,pop()
删除数组中的最后一项。
为了从数组中获得第一项,请单击you need to use shift()
。
这将从数组中删除第一项,并返回它。
要解释shift()
的工作方式,请尝试运行以下代码:
let a = [ {"a":1}, {"b":2}, {"c":3} ];
// Prints [ {"a":1}, {"b":2}, {"c":3} ]
console.log(a);
// This line will remove the top item from a
// and store it in topItem
let topItem = a.shift();
// Prints {"a":1}
console.log(topItem);
// Prints [ {"b":2}, {"c":3} ]
console.log(a);