1

I am using lowdb to pop an object from a list

{
  "posts": [
    { "id": a, "title": "lowdb is awesome"},
    { "id": b, "title": "lowdb is awesome"},
    { "id": c, "title": "lowdb is awesome"}
  ],
  "user": {
    "name": "typicode"
  },
  "count": 3
}

and need to figure out how to "pop" the first inserted object from posts:

db.get('posts')
  .find()
  .value()

which I expect to return { "id": a, "title": "lowdb is awesome"} and the posts will reflect this

user299709
  • 4,922
  • 10
  • 56
  • 88

2 Answers2

0

removing the first item in an array can simply be done by using shift() method. Note that pop() removes the last item in the array.

Josh
  • 827
  • 5
  • 7
0

In order to get the first item from an array, you need to use shift().

This will remove the first item from the array, and return it.

To explain how shift() works, try running the code below:

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);
Joundill
  • 6,828
  • 12
  • 36
  • 50