我需要从这种字符串中获取“付款类型和客户类型 Examples: | D | A | B | C | | NZ | AAA | BBB | NZ | | AZ | CCC | DDD | AZ | | CA | EEE | FFF | CA |我应该尝试获取模式并为此编写函数吗?或者我可以找到一些图书馆来检测它因此输出应为{付款:[“ AAA”,“ CCC”,'EEE'],客户:[“ BBB”,'DDD“,” FFF“]}function detect(str){ let countBar=1 let countBar2=0 let paymentLoc=NaN let customerLoc=NaN let after =0 let arr1=str.split(" ") arr1=arr1.filter(item=>{return item!==""}) let newStr='' for(let i=0;i<arr1.length;i++){ arr1[i].trim() if(arr1[i]==='|'){ countBar++ } if(arr1[i]==="||"){ countBar2++ } if(arr1[i].includes("payment")){ paymentLoc=i } after=((countBar/(countBar2))-1)*2let sol=[] for(let i=0;i<arr1.length;i++){ if(arr1[i].includes("payment")){ console.log('payment index',i) sol.push(arr1[i+after+1]) } if(arr1[i].includes("customer")){ console.log('customer index',i) sol.push(arr1[i+after+1]) } } newStr=arr1.join('') console.log(newStr)}
2 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
这是一个试图用|分开的尝试。
var text = `| market | payment type | customer type | translation |
| NZ | AAA | BBB | NZ |
| AZ | CCC | DDD | AZ |
| CA | EEE | FFF | CA |`;
var result = {
payment: text.split('|').filter((f, i) => i-7 >= 0 && (i - 7) % 5 == 0)
.map(p => p.trim()),
customer: text.split('|').filter((f, i) => i-8 >= 0 && (i - 8) % 5 == 0)
.map(p => p.trim())
}
console.log(result)
添加回答
举报
0/150
提交
取消