I need to extract all unique refIds from a nested object array, regardless of the level they are located at.
Attempting to loop through with for-loops has proven to be overly complex, especially since I'm unsure of the exact depth of the data. However, there is always a
"type": "text"
element with an optional "marks"
field.
[
{
"type": "bulletList",
"content": [
{
"type": "listItem",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"marks": [ // optional mark
{ "type": "refId", "attrs": { "refIds": [123, 234] } } // need this values
],
"text": "Item 1"
}
]
}
]
},
{
"type": "listItem",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"marks": [{ "type": "refId", "attrs": { "refIds": [987] } }],
"text": "Item 2"
}
]
}
]
},
{
"type": "listItem",
"content": [
{
"type": "paragraph",
"content": [{ "type": "text", "text": "Item 3" }] // has no mark
},
{
"type": "bulletList", // example for nested child
"content": [
{
"type": "listItem",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"marks": [
{ "type": "refId", "attrs": { "refIds": [876] } }
],
"text": "Sub 1"
}
]
}
]
}
]
}
]
}
]
}
]
The desired output should be:
["123", "234", "876", "987"]