如何从此数据获取此值

时间:2020-06-02 13:38:07

标签: javascript node.js reactjs express

envelope:'{"to":["reply-to-501-4455@email.ashishbhangade.com"],"from":"Tway"}'

在这里,我的对象数据和我只想查找值501和4455(每次此值都是动态的而不是静态值),所以如何获取请给我建议。

2 个答案:

答案 0 :(得分:2)

这是获取动态值的方法

const split = '{"to":["reply-to-501-4455@email.ashishbhangade.com"],"from":"Tway"}'.split('-')

const code1 = split[2]
const arrWithCode2 = split[3].split('@')
const code2 = arrWithCode2[0]

console.log(code1, code2)

如果“回复”和代码长度没有变化

const msg = '{"to":["reply-to-501-4455@email.ashishbhangade.com"],"from":"Tway"}'

const code1 = msg.substring(17, 20)
const code2 = msg.substring(21, 25)

console.log(code1, code2)

答案 1 :(得分:0)

也许用string.includes

const envelope = {"to":["reply-to-501-4455@email.ashishbhangade.com"],"from":"Tway"}

console.log(
envelope.to.some(pr => pr.includes("501")),
envelope.to.some(pr => pr.includes("4455"))
)

相关问题