How to change array fields in place?

Let's say I have this object:

{ "id": "1a48c847-4fee-4968-8cfd-5f8369c01f64" , "sections": [ { "id": 0 , "title": "s1" } , { "id": 1 , "title": "s2" } , { "id": 2 , "title": "s3" } ] } 

How can I directly change the second header of "s2" to a different value? without loading the object and saving again? Thanks.

+6
source share
1 answer

Update plus changeAt term:

 r.table('blog').get("1a48c847-4fee-4968-8cfd-5f8369c01f64").update(function(row){ return { sections: row('sections').changeAt(1, row('sections')(1).merge({title: "s2-modified"})) } } 

This is good if you already know the index of the item you want to change. If you need to find the index and then update it, you can use the .offsetsOf command to find the index of the item you need:

 r.table('table').get("1a48c847-4fee-4968-8cfd-5f8369c01f64").update(function(row){ return row('sections').offsetsOf(function(x){ return x('title').eq('s2') })(0).do(function(index){ return { sections: row('sections').changeAt(index, row('sections')(index).merge({title: "s2-modified"})) } }) }) 

Edit: modified response to use changeAt

+7
source

Source: https://habr.com/ru/post/979708/


All Articles