I have a piece of code here, and i wanted to identify the last element paragraph 3 and add some text like - last item and the output would be paragraph 3 - last item.
I would prefer if its recursive since there is no limit on the number children in an object.
obj = {
content: [
{ text: "paragraph 1" },
{
content: [
{ text: "paragraph 2" },
]
},
{ text: "paragraph 3" },
]
}
Another example would be this, its output should be paragraph 5 - last item
obj = {
content: [
{ text: "paragraph 1" },
{
content: [
{ text: "paragraph 2" }
]
},
{ text: "paragraph 3" },
{
content: [
{ text: "paragraph 4" },
{
content: [
{ text: "paragraph 5" }
]
}
]
}
]
}
function recur(content = []) { return content.map(Object.entries).map(([key, value]) => key === “text" ? [value] : key === "content" ? value.map(r) : []).flat(); }, then invoke it as inrecur(obj.content).sort((x, y) => x.localeCompare(y))[0] + “ - last"