posts - 59,comments - 0,views - 34881

前提:后端返回的数据内容

但是红框的数据对于前端来说是不正确的数据

所以我感觉前端处理这个数据本身这个操作都很傻X
但是我尝试进行转换代码如下

得到的数据:

点击查看代码
const interfaceDate = '"["2023-02-23","2023-02-28"]"' // 1. 将后端返回的数据用单引号进行包裹
    console.log('双引号的替换', interfaceDate.replace(/["]/g, ''))
    let handleDate = interfaceDate.replace(/["]/g, '').split(',') // 2. 将双引号进行替换为空,并进行切割成数组的形式
    console.log('有杂质的handleDate1', handleDate) // 当前的数据已经数组了,只是数据不纯粹,有杂质
    if (handleDate.length === 1) { //  但数据只有一个的‘"["2023-02-23"]"’
      const ht = handleDate[0].slice(1, 11)
      handleDate = []
      handleDate.push(ht)
      console.log('只有一个数据的时候的处理结果', handleDate)
    } else {
      //  但数据有多个的‘"["2023-02-23","2023-02-28"]"‘
      handleDate = handleDate.map((item) => {
        // 3. 将杂质的数据进行过滤
        if (item.includes('[')) {
          return item.replace('[', '')
        } else if (item.includes(']')) {
          return item.replace(']', '')
        }
        return item
      })
    }
    console.log('handleDate的类型', Array.isArray(handleDate))
    console.log('纯粹的handleDate', handleDate) // 4. 得到自己需要的数据
posted on   好久不见-库克  阅读(232)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示