khalby786 / jsoning

✨ A simple key-value JSON-based lightweight database. ✨
http://jsoning.js.org/
MIT License
91 stars 10 forks source link

Remove from array #20

Closed nrukavkov closed 2 years ago

nrukavkov commented 3 years ago

Following the instruction, we can add to the array. But I could not find a way how to remove an element from array.

// push stuff to an array for a particular key
    await db.push("transformers", "optimus prime");
    await db.push("transformers", "bumblebee");
    await db.push("transformers", "iron hide");
khalby786 commented 3 years ago

Sure, thanks for the suggestion, I'll add it!

On Fri, Sep 24, 2021, 7:55 PM Nikita Rukavkov @.***> wrote:

Following the instruction, we can add to the array. But I could not find a way how to remove an element from array.

// push stuff to an array for a particular key await db.push("transformers", "optimus prime"); await db.push("transformers", "bumblebee"); await db.push("transformers", "iron hide");

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/khalby786/jsoning/issues/20, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJFPUQ4KPWYG3WCJN2374OTUDSNQBANCNFSM5EWIA45Q .

Kerrders commented 3 years ago

I just use something like this:

const transformers = await db.get("transformers");
const transformerToRemove = "optimus prime";
await db.set(`transformers`, transformers.filter(transformer=> transformer !== transformerToRemove))
khalby786 commented 3 years ago

Is anyone interested in implementing this feature resulting in a Hacktoberfest PR? ;)

yucopowo commented 2 years ago
jsoning.prototype.remove = async function(key, callback) {
  const array = await this.get(key);
  return this.set(key, array.filter(item => !callback(item)))
};

await db.remove('jobs', (job) => {
  return job.id === 'xxxxxxx';
});

jsoning.prototype.update = async function(key, callback) {
  const array = await this.get(key);
  return this.set(key, array.map(item => callback(item) || item))
};
await db.update('jobs', (job) => {
  if(job.id === 'xxxxxxx') {
    return {
      ...job,
      ...params,
    }
  }
  return job;
});