I have an array which is of this structure:
this.state.mainArray= [{
"Upperelement1": "12345",
"Upperelement2" : [
{
Key1:'ok1',Key2:'ok2',Key3:'ok3'
},
{
Key1:'ok4',Key2:'ok6',Key3:'ok7'
},
]
},
{
"Upperelement1": "6789",
"Upperelement2" : [
{
Key1:'ok8',Key2:'ok9',Key3:'o10'
},
{
Key1:'ok11',Key2:'ok12',Key3:'ok13'
},
]
}
]
Idea is to iterate through the array and find element where Upperelement1 = 12345 and Key1:'ok1' (value of key is unique)
and add another key to Upperelement2, key4. After updation, array will look like:
[{
"Upperelement1": "12345",
"Upperelement2" : [
{
Key1:'ok1',Key2:'ok2',Key3:'ok3',Key4:'somevalue'
},
{
Key1:'ok4',Key2:'ok6',Key3:'ok7'
},
]
},
{
"Upperelement1": "6789",
"Upperelement2" : [
{
Key1:'ok8',Key2:'ok9',Key3:'o10'
},
{
Key1:'ok11',Key2:'ok12',Key3:'ok13'
},
]
}
]
I tried something like way:
mainArray.map(items => if (items. Upperelement1 == '12345')
- but that does not work, Any thoughts how this can be achieved ? Can we use filter ?