Best practise to change value in a objet located inside nested lists

I’ve a simple question. Supose you have the following object in a page variable. List ob objects with list of objectis inside. For the sample below, what is the best method or the best practise to change the value of age from 4 to 7 int he object id_parent=2, obj_array class=“middle”, id=2? What formula is the best apporachto use?

[{

id_parent”: 1,
“has_more_items”: false,
“data”: {

length”: 20,
“text”: “Lorem ipsum”,
“objArray”: [{

class”: “upper”,
“age”: 9,
“objDetailedArray”: [{

id”: 1,
“class”: “muchlower”,
“age”: 0
},
{

id”: 2,
“class”: “muchlower”,
“age”: 4
}
]
},
{

class”: “middle”,
“age”: 8,
“objDetailedArray”: [{

id”: 1,
“class”: “muchlower”,
“age”: 2
},
{

id”: 2,
“class”: “muchlower”,
“age”: 4
}
]
}
]
}
}, {

id_parent”: 2,
“has_more_items”: false,
“data”: {

length”: 20,
“text”: “Lorem ipsum”,
“objArray”: [{

class”: “upper”,
“age”: 9,
“objDetailedArray”: [{

id”: 1,
“class”: “muchlower”,
“age”: 0
},
{

id”: 2,
“class”: “muchlower”,
“age”: 4
}
]
},
{

class”: “middle”,
“age”: 8,
“objDetailedArray”: [{

id”: 1,
“class”: “muchlower”,
“age”: 2
},
{

id”: 2,
“class”: “muchlower”,
“age”: 4
}
]
}
]
}
}, {

id_parent”: 3,
“has_more_items”: false,
“data”: {

length”: 20,
“text”: “Lorem ipsum”,
“objArray”: [{

class”: “upper”,
“age”: 9,
“objDetailedArray”: [{

id”: 1,
“class”: “muchlower”,
“age”: 0
},
{

id”: 2,
“class”: “muchlower”,
“age”: 4
}
]
},
{

class”: “middle”,
“age”: 8,
“objDetailedArray”: [{

id”: 1,
“class”: “muchlower”,
“age”: 2
},
{

id”: 2,
“class”: “muchlower”,
“age”: 4
}
]
}
]
}
}]

Hi Antonio, I would use SET_KEY inside MAP for this. Something like:

MAP(pageVars.objects, IF(item.id_parent === 2 && item.obj_array.class === "middle" && item.obj_array.id === 2, SET_KEY(item, "age", 7), item))

So you go over the objects with MAP, check if the item matches your criteria, if it does you use SET_KEY to manipulate the object, and if not just return the object itself in the new array created by MAP. Not completely sure if that formula will pass since I just wrote it up but the general idea is there. :smile:

Docs: MAP, SET_KEY

Also just checking this formula would be used inside a “set app/page/data variable” logic flow element?

Yes exactly, it creates a new array that can then be set as the variable’s new value.

1 Like

This approach it’s very interesting. I’ve a couple of question about this.

Number 1:
If you will try to change the 3child “id” property (parent and firstchild have an id property too) how do you think it’s the correct procedure to do this in the MAP formula? Because if you’ll try this:

MAP(pageVars.objects, IF(item.id ==0 && item.fooparent.id ==1 && item.fooparent.foofirstchild==1, SET_KEY(item, “id”, 3), item))
The property id changed is the id:0… and if you try: …SET_KEY(item.fooparent.foofirstchild, “id”, 3) the return of the function it’s only the last object, dropped the entire structure.

For Example, trying to change with MAP, only inside foofirstchild, id from 2 to 3

pagevar.someobject:
[{
id: 0
fooparent: {
id = 1,
foofirstchild: [{
id: 2
foosecondhild: bar
}]
}
}]

Number 2
Is it possible this sentence "item.obj_array.class === “middle” with the fact of obj_array is a list inside the first list? Is not necessary any index or similar like item.obj_array[anyindex].class keeping in mind MAP (If I understand well) is like an iteration inside of the objects tree?

King Regards

Hi Antonio, you’re right, I just quickly replied so I overlooked some of the complexity of the case. Sorry for that!

The basic idea for doing this with nested lists is the same, you just chain the MAP and SET_KEY formulas with aliases to access the different level lists. Like this:

MAP<student>(pageVars.studentInfo, SET_KEY(student, "student.data.objArray", MAP<obj>(student.data.objArray, SET_KEY(obj, "obj.objDetailedArray", MAP<detail>(obj.objDetailedArray, detail)))))

That formula doesn’t do anything else but sets the value of a detail object in the objDetailedArray as itself, but it demonstrates the correct syntax. To implement choosing the item whose property to change you can use IF(condition, SET_KEY(...), item) on the different levels of iteration like in the example I gave in my first message. This way MAP only continues iterating deeper into the nested lists for those objects that match the criteria on that level of the tree.

Hope this clarifies!

Awesome! Nested MAPS may be works. I’ll try later and I comment you. Thanks!!!

It would be a nice feature if Appgyver expands the current Set Variable nodes to allow selecting a child / sub variable. This would be super elegant then.

Right now the Set Variable node only allows to select the top variable.

More specifically:

  • Set Variable node allows to select sub variables if top variable is an object.
  • If top variable is a list, then it’s currently not possible to set value for a list element with the Set Variable node. That can be done with the Javascript node but quite cumbersome.
  • It’s desirable for the Set Variable node to set value of a list element.