I have a complex structure of objects containing arrays, nested within other objects. Here is an example of the structure:
My goal is to determine the longest array of objects within each parent object. Using the example below:
For ObjectOne - the longest array is B.
For ObjectTwo, the longest array is C.
mainobj = {
ObjectOne: {
A: [
{ x: 'd', y: 'e'},
{ x: 'd', y: 2}
],
B: [
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'}
],
C: [
{ x: 'd', y: 'e'}
]
},
ObjectTwo:{
A: [
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'}
],
B: [
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'}
],
C: [
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'},
{ x: 'd', y: 'e'}
]
}
}
To implement this logic:
var size = Object.values(mainobj).length
However, accessing nested arrays requires a different approach, like this:
var size = Object.values(mainobj)[0].length
If you're looking for help on a similar issue, check out this question. Thank you in advance for your assistance!