后端返回数据的一些处理

  1. 字符串转化为数组格式
    后端返回数据:
    在前端需循环遍历params数据
{"id":"821039581393584128","attrTitle":"扩展属性0102","params":"['kz010201','kz010202']","useIs":1}

转换方法:

const data = {"id":"821039581393584128","attrTitle":"扩展属性0102","params":"['kz010201','kz010202']","useIs":1}
newParams = (((JSON.stringify(this.data.params)).replace(/\"/g, '')).trim()).replace(/'/g, '"')
 this.params = JSON.parse(newParams)
  1. 将数组按照2个一组的二维数组
attrList:[
 {
  "attrKey":"PZ-055",
  "attrTitle":"扩展属性0102",
 "attrType":"6001302",
  "groupId":"821039255865262080",
  "groupKey":"PG-015",
  "id":821039581393584100,
  "params":["kz010201","kz010202"],
  "useIs":1,
  "val":""
},
{
  "attrKey":"PZ-054",
  "attrTitle":"扩展属性0101",
  "attrType":"6001301",
  "groupId":"821039255865262080",
  "groupKey":"PG-015",
  "id":821039396777099300,
  "params":[],
  "useIs":1,
  "val":""
 }
]
attrList = group(attrList, 2)
group(array, subGroupLength) {
      let index = 0
      const newArray = []
      while (index < array.length) {
        newArray.push(array.slice(index, index += subGroupLength))
      }
      if (array.length % 2 !== 0) {
        newArray[newArray.length - 1].push({ 'attrTitle': null })
      }
      return newArray
    }
attrList:
[
 [
  {
   "attrKey":"PZ-055",
   "attrTitle":"扩展属性0102",
   "attrType":"6001302",
   "groupId":"821039255865262080",
   "groupKey":"PG-015",
   "id":821039581393584100,
   "params":["kz010201","kz010202"],
   "useIs":1,
   "val":""
 },
 { 
    "attrKey":"PZ-054",
    "attrTitle":"扩展属性0101",
    "attrType":"6001301",
    "groupId":"821039255865262080",
    "groupKey":"PG-015",
    "id":821039396777099300,
    "params":[],
    "useIs":1,
    "val":""
  }
 ]
]

你可能感兴趣的:(后端返回数据的一些处理)