我如何用唾液和熔接递归更新数组?

时间:2018-07-23 00:16:30

标签: javascript arrays json replace

我真的很感谢您的帮助-预先感谢您!

代码需要执行以下操作:

它需要遍历[text]数组。只要找到%${extLinks[i].subject}%的地方,都应使用对象{subject: extLinks[i].subject, target: extLinks[i].target}替换该文本并将其添加到数组中。

编辑: ---此外,只应替换subject+target的FIRST实例,而忽略其他实例。

它也必须可重用-在整个数据库中使用subject+target的不同组合(大约有700个链接的主题!)---

这就是我要去的地方

addLinks(synopsis, extLinks = []) {
    let text = synopsis
    for (let i = 0; i < extLinks.length; i++) {
      text = text.replace(extLinks[i].subject, `%${extLinks[i].subject}%`)
    }
    let items = [text]

    extLinks.map(link => {
      let arr = []
      for (var j = 0; j < items.length; j++) {
        console.log(items)
        arr = items[j].split(`%${link.subject}%`)
        arr.splice(1, 0, {
          subject: extLinks[j].subject,
          target: extLinks[j].target,
        })
        items = arr.splice(0)
      }
    })

    console.log({ items })

    return [synopsis]
  }

预期收益是这样的:

["Striking from a fortress hidden among the billion stars of the ", {"subject": "GALAXY", "target": "https://teara.govt.nz/mi/te-mana-o-te-wahine/page-1"}, ", rebel spaceships have won their first victory in a battle with the powerful Imperial Starfleet.  The ", { "subject": "EMPIRE", "target": "https://teara.govt.nz/mi/te-mana-o-te-wahine"  }, "  fears that another defeat could bring a thousand more solar systems into the rebellion, and Imperial control over the GALAXY would be lost forever. Here's another sentence with EMPIRE in it."]

但是当前代码正在返回此:

["Striking from a fortress hidden among the billion stars of the %GALAXY%, rebel spaceships have won their first victory in a battle with the powerful Imperial Starfleet.  The %EMPIRE% fears that another defeat could bring a thousand more solar systems into the rebellion, and Imperial control over the GALAXY would be lost forever. Here's another sentence with EMPIRE in it."]

如此-它无法正确创建最终数组,并且与我需要递归更新数组的方式有关。

fyi我正在使用的数据库对象如下:

"content": { ... "synopsis": { ... "long": "Striking from a fortress hidden among the billion stars of the GALAXY, rebel spaceships have won their first victory in a battle with the powerful Imperial Starfleet. The EMPIRE fears that another defeat could bring a thousand more solar systems into the rebellion, and Imperial control over the GALAXY would be lost forever. Here's another sentence with EMPIRE in it.", ... ] }, "external links": [ { "subject": "EMPIRE", "target": "https://teara.govt.nz/mi/te-mana-o-te-wahine" }, { "subject": "GALAXY", "target": "https://teara.govt.nz/mi/te-mana-o-te-wahine/page-1" } ],

1 个答案:

答案 0 :(得分:1)

怎么样?

function injectLinksIntoSynopsis (synopsis = '', links = []) {
  const linkDictionary = links.reduce((obj, link) => {
    obj[link.subject] = link
    return obj
  }, {})
  const linkMatch = new RegExp(`(${links.map(link => link.subject).join('|')})`, 'g')
  return synopsis
    .split(linkMatch)
    .map(val => {
      return linkDictionary[val] || val
    })
}

基本上

  1. 将所有链接主题转换为包含性的正则表达式/(GALAXY|EMPIRE)/g。这意味着当您分割字符串时,您还将在正则表达式中获得匹配的值。
  2. 将您的synopsis拆分为链接值和非链接值的数组
  3. 创建链接字典以查找链接(比使用索引更干净,更容易阅读)。 {GALAXY: {subject: 'GALAXY', target: 'https://teara.govt.nz/mi/te-mana-o-te-wahine/page-1'}}
  4. 然后映射每个值并返回它或它在字典中的映射值。

这里的工作示例https://jsfiddle.net/stwilz/9342nzj5/52/


如果您只想替换匹配项的第一个实例,像这样的爵士乐,

function injectFirstLinkIntoSynopsis (synopsis = '', links = []) {
  let stringifiedArray = links.reduce((newSynopsis, link) => {
    return newSynopsis.replace(link.subject, `", ${JSON.stringify(link)}, "`)
  }, `["${synopsis}"]`)
  return JSON.parse(stringifiedArray)
}

我们遍历您的链接,并将其主题的第一个实例替换为stringified对象。完成后,我们只需JSON.parse()将其返回到数组即可。

我个人是该解决方案的忠实拥护者,因为我滥用了JSON API:)

这里有新的工作示例https://jsfiddle.net/stwilz/9342nzj5/76/