I've data which I need to break it down into multiple objects and return it.
Below is the sample data
const DATA = {
text: "\nDo you have questions or comments and do you wish to contact ABC? Please visit our customer support page.",
EntityRanges: [
{
type:"LINK",
offset:2,
length:2,
data: { target:null, url:"/index.html", description:null }
},
{
type:"LINK",
offset:84,
length:16,
data: { target:null, url:"/index.html", description:null }
}
]
};
Now, I loop EntityRanges and for each item, I check offset & length and break the text.
Stackblitz Link to my working example.
As per the example, text should be broken down like the following
- Do, type 'link'
- you have questions or comments and do you wish to contact ABC? Please visit our , type 'text'
- customer support, type 'link'
- page., type 'text'
So my expected output should be like this,
[
{
"type": "LINK",
"text": "Do",
"data": {
"target": null,
"url": "/index.html",
"description": null
}
},
{
"type": "TEXT",
"text": "you have questions or comments and do you wish to contact ABC? Please visit our ",
"data": {}
},
{
"type": "LINK",
"text": "customer support ",
"data": {
"target": null,
"url": "/index.html",
"description": null
}
},
{
"type": "text",
"text": " page.",
"data": {},
}
]
But I'm not getting the expected result. Please Help.
UPDATE
Based on the below solutions, I've still have some issue.
offset Value will always start from 0.
text: "Do you have questions or comments and do you wish to contact KLM? Please visit our customer support page.",
Offset: 0,Length: 2 - Do
Offset: 83, Length: 16 - customer support
But I get the below output,
[
{
"type": "link",
"text": "D",
"data": {
"target": null,
"url": "/index.html",
"description": null
}
},
{
"type": "text",
"text": " you have questions or comments and do you wish to contact ABC? Please visit our",
"data": {}
},
{
"type": "link",
"text": " customer suppor",
"data": {
"target": null,
"url": "/index.html",
"description": null
}
},
{
"type": "text",
"text": " page",
"data": {}
}
]
Example in the stackBlitz