javascript - 根据输入将字符串分解为多个对象
问题描述
我有需要将其分解为多个对象并返回的数据。
下面是样本数据
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 }
}
]
};
现在,我循环EntityRanges
并为每个项目检查offset
&length
并打破文本。
根据示例,文本应按如下方式分解
- 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'
所以我的预期输出应该是这样的,
[
{
"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": {},
}
]
但我没有得到预期的结果。请帮忙。
更新
基于以下解决方案,我仍然有一些问题。
offset
价值总是从 开始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
但我得到以下输出,
[
{
"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": {}
}
]
stackBlitz中的示例
解决方案
只需使用它:子字符串
这是一个丑陋的例子,只是为了解释一个可能的解决方案。
const DATA = {
text: "Do you have questions or comments and do you wish to contact ABC? " +
"Please visit our customer support page.",
EntityRanges: [
{
type:"LINK",
offset:0,
length:2,
data: { target:null, url:"/index.html", description:null }
},
{
type:"LINK",
offset:83,
length:16,
data: { target:null, url:"/index.html", description:null }
}
]
};
var last = 0, result = [];
DATA.EntityRanges.forEach(function(e) {
var text = DATA.text.substring(last, e.offset);
if (text != "") {
result.push({"type": "text", "text": text, data: {}});
}
result.push({"type": "link", "text": DATA.text.substring(e.offset, e.offset+e.length), "data": e.data});
last = e.offset+e.length;
});
var text = DATA.text.substring(last, DATA.text.length-1);
if (text != "") {
result.push({"type": "text", "text": text, data: {}});
}
console.log(result);
推荐阅读
- autocad - 添加 MTEXT 时 Autocad 不读取我的 DXF 文件
- c++ - 为什么 std::copy 以相反的顺序
- c# - SQL Server 插入(如果不存在)
- asp.net - TinyMCE - 未捕获的类型错误:部署到 Azure 网站时无法读取未定义的属性“工厂”
- html - 带有 Chrome 的网格元素的随机大小
- java - 如何将异常的详细跟踪放入变量中?
- machine-learning - 除了 VAR、VARMA 之外,还有其他模型可用于预测多变量时间序列数据吗?
- python - 如何从python字符串中提取字符串?
- azure-web-app-service - 我在 azure python webapp 上收到 JAVA_HOME not set 错误
- robotframework - Robot Framework - 驱动 Firefox 浏览器抛出 ssh 连接