对不起,用这种格式的数据,如何根据给定规则切割字符串

发布于 2025-02-12 01:29:50 字数 812 浏览 0 评论 0原文

对不起,用这种格式的数据,如何根据给定规则

数据切割字符串:

切割字符串

{
         'content': 'key1vip has been serving you for 2 days, and the customer service will provide you with professional answers and formulate solutions',
         'keywords': [{
           'keyword': 'key1',
           'replaceKeyword': 'On',
           'link': '',
           'color': '',
         }, {
           'keyword': 'key2',
           'replaceKeyword': '30',
           'link': '',
           'color': '',
         }]
       }

如何根据给定规则转换为以下格式

const _array = [
  {text: 'Already for you'},
  {text: 'on', link: '', color: ''},
  {text: 'vip service'},
  {text: '30', link: '', color: ''},
  {text: 'days, customer service will provide you with professional answers and make plans'},
]

Excuse me, with data in this format, how to cut the string according to the given rules

data:

how to cut the string according to the given rules

{
         'content': 'key1vip has been serving you for 2 days, and the customer service will provide you with professional answers and formulate solutions',
         'keywords': [{
           'keyword': 'key1',
           'replaceKeyword': 'On',
           'link': '',
           'color': '',
         }, {
           'keyword': 'key2',
           'replaceKeyword': '30',
           'link': '',
           'color': '',
         }]
       }

Convert to the following format

const _array = [
  {text: 'Already for you'},
  {text: 'on', link: '', color: ''},
  {text: 'vip service'},
  {text: '30', link: '', color: ''},
  {text: 'days, customer service will provide you with professional answers and make plans'},
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一腔孤↑勇 2025-02-19 01:29:50

以下方法对您的当前数据格式有效。您需要调试其他格式。

function transformText(text, keywords) {
  const indexes = keywords.reduce((res, v, i) => (res[v.keyword] = i, res), {})
  const kwReg = RegExp(keywords.map(v => v.keyword).join('|'), 'g');
  const res = [];
  var index = 0;
  text.replace(kwReg, (match, offset) => {
    const kw = keywords[indexes[match]]
    res.push(
      {text: text.slice(index, offset)},
      {text: kw.replaceKeyword, link: kw.link, color: kw.color}
    )
    index = offset+match.length
    return ''
  })
  if(index<text.length) res.push({text: text.slice(index)})
  return res
}

var data = {
         'content': 'key1vip has been serving you for 2 days, and the customer service will provide you with professional answers and formulate solutions',
         'keywords': [{
           'keyword': 'key1',
           'replaceKeyword': 'On',
           'link': 'baidu.com',
           'color': 'red',
         }, {
           'keyword': 'key2',
           'replaceKeyword': '30',
           'link': 'bing.com',
           'color': 'blue',
         }]
       }

transformText(data.content, data.keywords)

The following method is valid for your current data format. You need to debug other formats.

function transformText(text, keywords) {
  const indexes = keywords.reduce((res, v, i) => (res[v.keyword] = i, res), {})
  const kwReg = RegExp(keywords.map(v => v.keyword).join('|'), 'g');
  const res = [];
  var index = 0;
  text.replace(kwReg, (match, offset) => {
    const kw = keywords[indexes[match]]
    res.push(
      {text: text.slice(index, offset)},
      {text: kw.replaceKeyword, link: kw.link, color: kw.color}
    )
    index = offset+match.length
    return ''
  })
  if(index<text.length) res.push({text: text.slice(index)})
  return res
}

var data = {
         'content': 'key1vip has been serving you for 2 days, and the customer service will provide you with professional answers and formulate solutions',
         'keywords': [{
           'keyword': 'key1',
           'replaceKeyword': 'On',
           'link': 'baidu.com',
           'color': 'red',
         }, {
           'keyword': 'key2',
           'replaceKeyword': '30',
           'link': 'bing.com',
           'color': 'blue',
         }]
       }

transformText(data.content, data.keywords)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文