Return to parent object in lodash chaining "_.chain()"

I’m using a library (lowdb) that uses lodash under the hood to make a local database in a json file. The thing I want to know is how to get to the parent object in lodash chaining after modifying child to modify another one, eg.

const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');

const adapter = new FileSync('db.json');
const db = low(adapter);

/*
Eg. DB structure:
{
  "posts": [
    {
      "id": 1,
      "title": "post1"
    },
    {
      "id": 2,
      "title": "post2"
    },
    {
      "id": 3,
      "title": "post3"
    }
  ]
}
*/

db
  .get('posts')
  .find({ id: 2 })
  .assign({ title: 'edited title' }) 
  //after that I want to go back to posts to edit another one in the same chain
  .write();

I know that it could be done in multiple calls but I was wondering if it’s possible to do it in one chain.

117 thoughts on “Return to parent object in lodash chaining "_.chain()"”

  1. 164615 552659This style is steller! You surely know how to maintain a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (effectively, almostHaHa!) Amazing job. I genuinely enjoyed what you had to say, and much more than that, how you presented it. Too cool! 68920

    Reply

Leave a Comment