I have the next array structure:
const arr = [
{
label: 'Apple',
data: [{test: '1'}, {test: '2'}],
collapsedData: [{test: '1'}, {test: '2'}],
},
{
label: 'Orange',
data: [{test: '3'}, {test: '4'}],
collapsedData: [{test: '3'}, {test: '4'}],
},
{
label: 'Tomato',
data: [{test: '5'}, {test: '6'}],
collapsedData: [{test: '5'}, {test: '6'}],
},
{
label: 'Banana',
data: [{test: '7'}, {test: '8'}],
collapsedData: [{test: '7'}, {test: '8'}],
}
]
I also have a method to sort an array - sortArr
- sorts an array by alphabet.
I need to iterate over arr
and apply sortArr
to data
and collapsedData
if the label is Apple
.
So, data
and collapsedData
should be sorted for Apple
label.
Desired output:
const outputArr = [
{
label: 'Apple',
data: [{test: '2'}, {test: '1'}],
collapsedData: [{test: '2'}, {test: '1'}],
},
{
label: 'Orange',
data: [{test: '3'}, {test: '4'}],
collapsedData: [{test: '3'}, {test: '4'}],
},
{
label: 'Tomato',
data: [{test: '5'}, {test: '6'}],
collapsedData: [{test: '5'}, {test: '6'}],
},
{
label: 'Banana',
data: [{test: '7'}, {test: '8'}],
collapsedData: [{test: '7'}, {test: '8'}],
}
]
What can be the solution?