首页 > 解决方案 > 根据输入将字符串分解为多个对象

问题描述

我有需要将其分解为多个对象并返回的数据。

下面是样本数据

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并打破文本。

Stackblitz 链接到我的工作示例

根据示例,文本应按如下方式分解

- 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中的示例

标签: javascriptarraystypescriptmultidimensional-array

解决方案


只需使用它:子字符串

这是一个丑陋的例子,只是为了解释一个可能的解决方案。

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);


推荐阅读