首页 > 精简字符串,写了好几个JS处理函数,都没解决,卡了好几天了

精简字符串,写了好几个JS处理函数,都没解决,卡了好几天了

写了好多函数,结果卡住了没有找到什么好方法。

其实就是我项目中用到流程图,由于是一些动态生成的,我想把它精简了。

比如有一个这样的字符串

st->io
io->cond
cond(yes)->op1
cond(no)->op2
op1->sop1
op2->sop2
sop1->io1
sop2->io2
io1->e1
io2->e2
e1->e
e2->e

我想精简成如下这种格式,就是精简到不能再精简

st->io->cond
cond(yes)->op1->sop1->io1->e1->e
cond(no)->op2->sop2->io2->e2->e

甚至是这种格式

st->io->cond(yes)->op1->sop1->io1->e1->e
cond(no)->op2->sop2->io2->e2->e

希望大家帮个忙,就是一个这样的功能,卡了好长时间了,不知道怎么弄了


你确定这些数据一定是一对一的么,,不会生成图?


抱歉,发错地方了。


:)

var originString = [
  "st->io",
  "io->cond",
  "cond(yes)->op1",
  "cond(no)->op2",
  "op1->sop1",
  "op2->sop2",
  "sop1->io1",
  "sop2->io2",
  "io1->e1",
  "io2->e2",
  "e1->e",
  "e2->e"
]

var first = []
var second = []
var result = []
var resultIndex = 0

var findMatch = function(a, e) {
  return a.indexOf(e)
}

var findConditionYes = function(a, e) {
  return a.indexOf(e + "(yes)")
}

var findConditionNo = function(a, e) {
  return a.indexOf(e + "(no)")
}

var getEmptyIndex = function(result) {
  for (var i = 0; ; i++) {
    if (result[i] === undefined) return i
  }
}

var calc = function(frist, second, index, resultIndex) {
  if (index === -1) return

  result[resultIndex] = result[resultIndex] || []
  if (result[resultIndex].length === 0 || result[resultIndex][result[resultIndex].length - 1] !== first[index]) {
    result[resultIndex].push(first[index])
  }
  result[resultIndex].push(second[index])
  // first.splice(index, 1)
  // second.splice(index, 1)

  var nextIndex = findMatch(first, result[resultIndex][result[resultIndex].length - 1])

  if (nextIndex !== -1) {
    calc(first, second, nextIndex, resultIndex)
  } else if (first.length !== 0) {
    var nextYesIndex = findConditionYes(first, result[resultIndex][result[resultIndex].length - 1])  
    calc(first, second, nextYesIndex, getEmptyIndex(result))

    var nextNoIndex = findConditionNo(first, result[resultIndex][result[resultIndex].length - 1])
    calc(first, second, nextNoIndex, getEmptyIndex(result))
  }

  return
}

originString.forEach(v => {
  var e = v.split("->")
  first.push(e[0])
  second.push(e[1])
})

calc(first, second, 0, 0)

result.forEach(v => {
  console.log(v.join("->"))
})

结果:

st->io->cond
cond(yes)->op1->sop1->io1->e1->e
cond(no)->op2->sop2->io2->e2->e

应题主改了个“加长版本”:

var originString = [
  "st->io->cond",
  "cond->op1->sop1->io1->e1->e",
  "e->op2->sop2->io2->e2->last"
]

var first = []
var second = []
var result = []
var resultIndex = 0

var findLast = function(e) {
  var a = e.split("->")
  return a[a.length - 1]
}

var findMatch = function(a, e) {
  return a.indexOf(findLast(e))
}

var findConditionYes = function(a, e) {
  return a.indexOf(findLast(e + "(yes)"))
}

var findConditionNo = function(a, e) {
  return a.indexOf(findLast(e + "(no)"))
}

var getEmptyIndex = function(result) {
  for (var i = 0; ; i++) {
    if (result[i] === undefined) return i
  }
}

var calc = function(frist, second, index, resultIndex) {
  if (index === -1) return

  result[resultIndex] = result[resultIndex] || []
  if (result[resultIndex].length === 0 || findLast(result[resultIndex][result[resultIndex].length - 1]) !== first[index]) {
    result[resultIndex].push(first[index])
  }
  result[resultIndex].push(second[index])
  // first.splice(index, 1)
  // second.splice(index, 1)

  var nextIndex = findMatch(first, result[resultIndex][result[resultIndex].length - 1])

  if (nextIndex !== -1) {
    calc(first, second, nextIndex, resultIndex)
  } else if (first.length !== 0) {
    var nextYesIndex = findConditionYes(first, result[resultIndex][result[resultIndex].length - 1])  
    calc(first, second, nextYesIndex, getEmptyIndex(result))

    var nextNoIndex = findConditionNo(first, result[resultIndex][result[resultIndex].length - 1])
    calc(first, second, nextNoIndex, getEmptyIndex(result))
  }

  return
}

originString.forEach(v => {
  var e = v.split("->")
  first.push(e[0])
  var last = e.slice(1).join("->")
  second.push(last)
})

calc(first, second, 0, 0)

result.forEach(v => {
  console.log(v.join("->"))
})

结果:

st->io->cond->op1->sop1->io1->e1->e->op2->sop2->io2->e2->last
【热门文章】
【热门文章】