【问题标题】:How to get this values form this data [closed]如何从此数据中获取此值 [关闭]
【发布时间】:2020-09-21 00:00:16
【问题描述】:
envelope:'{"to":["reply-to-501-4455@email.ashishbhangade.com"],"from":"Tway"}'

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

【问题讨论】:

    标签: javascript node.js reactjs express


    【解决方案1】:

    这是动态值的方式

    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)

    【讨论】:

    • 是的,但是如果它的值长度增加,那么这个值是动态的,那么它就不起作用了。谢谢!
    • 我添加了新的解决方案!
    【解决方案2】:

    也许是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"))
    )

    【讨论】:

    • 是的,我同意,但这是动态值而不是静态值。
    • 你的意思是整个对象结构可能会改变?
    • 不是只有“回复-”是静态的,其他都是动态的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多