js 中文属性和值的数据转换

今天服务端返回一个奇葩数据,类似下面这样:

const arr = [
      {
        '备注': 'a111',
        '当前状态': 'a222',
        '数据来源单位': 'a333'
      },
      {
        '备注': 'b111',
        '当前状态': 'b222',
        '数据来源单位': 'b333'
      },
      {
        '备注': 'c111',
        '当前状态': 'c222',
        '数据来源单位': 'c333'
      }
    ]

但是我们使用vue的v-for指令时没办法循环,所以需要处理一下上面的数据,下面我写一下方法

 created() {
    const arr = [
      {
        '备注': 'a111',
        '当前状态': 'a222',
        '数据来源单位': 'a333'
      },
      {
        '备注': 'b111',
        '当前状态': 'b222',
        '数据来源单位': 'b333'
      },
      {
        '备注': 'c111',
        '当前状态': 'c222',
        '数据来源单位': 'c333'
      }
    ]
    this.arr = this.getNewArr(arr)
},  
methods: {
    // 这里是child数组
    getChild(obj) {
      const resultArr = []
      Object.keys(obj).forEach((key, index, object) => {
        const tempObj = {}
        tempObj.name = key
        tempObj.value = obj[key]

        resultArr.push(tempObj)
      })

      return resultArr
    },
    getNewArr(arr) {
      const tempArr = []
      for (const i in arr) {
        const obj = arr[i]
        tempArr.push({
          child: this.getChild(obj)
        })
      }
      return tempArr
    }
  }

然后我们在模板里面这样去循环:

 <card v-for="(item,index) in arr" :key="index">
        <van-cell
          v-for="(child,cIndex) in item.child"
          :key="cIndex"
          :title="child.name"
          :value="child.value"
        />
 </card>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值