Search code examples
javascriptrethinkdbreql

Rethinkdb remove string from array


Given an array and data structure as below, I want to remove any values that match from the removeVals from the table field tags. I've found a solution for changing, and tried with the deleteAt, but got an indexesOf is not a function error.

var removeTags = ["val1", "val3"];

{
  name: "test1",
  tags: ["val1", "val2", "val3", "val4", "val5"]
}

so I should be left with:

{
  name: "test1",
  tags: ["val2", "val4", "val5"]
}

My code so far:

var removeTags = ["val1", "val2"]
r.db('test').table('data').get('test1').update(function(row) {
  return row('tags').indexesOf(function(rl) {
    return r.expr(removeTags).contains(rl)
  })(0).do(function(idx) {
    return {
      roles: row('tags').deleteAt(idx)
    }
  })
});

This is the question/answer I was using as a reference. Any help would be appreciated, many thanks!


Solution

  • Nevermind, I've found the answer and it was far easier than I thought, I was over complicating things too much!

    var removeTags = ['val1', 'val2'];
    r.db('test').table('data').get('test1')
      .update({tags: r.row('tags').difference(removeTags)});
    

    Anyway, hope this helps others