有这个代码
const myMagic = (one, two, three, four) => `this is ${one} and ${two} and ${three} and ${four} as usual`
const txt = 'HELLO&ho&hy&hu&hq&HELLO&ho&hy&hu&hq&HELLO&ho&hy&hu&hq&HELLO&ho&hy&hu&hq&hx'
const fragments = txt.split('&')
const pieces = []
for (let i=0; i<fragments.length-1;i +=5) {
pieces.push(fragments[i])
pieces.push(myMagic(fragments[i+1],fragments[i+2],fragments[i+3],fragments[i+4]))
}
pieces.push(fragments[fragments.length-1])
console.log(pieces)
如何将其转换为更具声明性的版本?
代码是这样的,因为拆分采用了一个只解析一次文本的正则表达式,然后使用这些片段,我正在构建尽可能多的组件与myMagic
函数
那么有没有办法在不改变逻辑的情况下以更具声明性的方式编写它?