I have a array as follows:
myArray = [
{
"id": 1,
"data": [
{
"active":1,
"dataId":1
},
{
"active":0,
"dataId":2
},
{
{
"active":1,
"dataId":3
}
}
]
}
]
Above is just sample array, at run time it can have many elements. I have a method as follows which receives id, dataId and active(value of active can be 0 or 1) as argument.
myMethod(id, dataId, activeValue) {
}
When this Id and dataId is received, I need find element with that id, then go inside data array and find element with dataId and set value of active attribute with the active value received in argument.
My try:
myMethod(id, dataId, activeValue) {
this.myArray.find(item => item.id === id).data[dataId].active = activeValue
}
But this code is not updating value. How can I do that?
this.myArray.find(x => x.id === id)[0].data.find(x => x.dataId === dataId)[0].active = activeValue..data[dataId]-> you have an array of objects, you cannot just use the index. Moreover you already know how to lookup something by a property. You already have the code for finding an item by itsid. Do the same but fordataIdinstead.