Which is the best way to set the values in child table?

I have child table in doctype Which one is best way to set values?

var data = [{"name":"test","age":"1"},{"name":"test1","age":"1"}]

#method 1
frm.set_value("fieldname",data)

#method 2
for(var i = 0; i < data.length; i++){
    var row = frm.add_child("fieldname",{
        "name":data[i].name,
        "age":data[i].age
    })
  }
frm.refresh_field("fieldname")

and also python?

Hi @NELSON_D1 :

It depends of many factors. To fill the whole child at once, maybe method 1.
frm.add_child lets you adds data row by row.

From backend can use
https://frappeframework.com/docs/user/en/api/document#doc-append

Hope this helps.

in my parent doctype i have a table with values like this:


Here, i want to filter this values, then set them to another tables
like there is total of orange color is 8, so i want to set another table like orange total is 8,

how do I do this, i create the button below this table, click the button, it filter the table then set
best practice for setting values from the backend or frontend??

Hi @NELSON_D1:

Again … it depends.

I would prefeer do it calling to backend method … if you don’t need to show this sumarized/filtered data on front side.