需求

在使用Antd-vue的Table组件时,数据dataSource为数组类型,现在使用查询接口的时候返回的时json对象,需要前台进行转化为json数组

获取到的数据如下,需要将result对象转成json数组

{
    "resultCode": 0,
    "result": {
        "createDate": null,
        "creatorId": null,
        "creatorName": null,
        "updateDate": null,
        "updatorId": null,
        "updatorName": null,
        "userId": "jGswoGLzicugeAWdLeiEUouTjJngc5XE",
        "dataRegion": "JD",
        "loginId": "liucuiqin",
        "userName": "刘翠琴",
        "domain": null,
        "loginCode": null,
        "birthday": null,
        "gender": null,
        "jobTitle": null,
        "post": null,
        "belongOrg": "ORGAJD100000023",
        "password": null,
        "userType": null,
        "emailAddress": null,
        "mobileNum": null,
        "userLogo": null,
        "orderNum": null,
        "status": 1,
        "lastLoginDate": null,
        "lastLoginIp": null,
        "lastFailedLoginDate": null,
        "failedLoginAttempts": null,
        "lockout": null,
        "lockoutdate": null,
        "comments": null
    },
    "msgId": null,
    "success": true
}

核心代码

export function objectConvertArray (data) { // data:需要转换的对象数据
  var tempArray = [] //临时数据
  var tempObject = {} // 临时对象
  for (var i in data) {
    tempObject[i] = data[i]
  }
  tempArray.push(tempObject)
  console.log(tempObject)
  return tempArray // 返回构建好的数组
}

调用转化函数测试

_this.data = objectConvertArray(res.result)

表数据正常渲染显示,说明成功将JsonObject转化为JsonArray

验证

前台查询 loginIdliucuiqin 的用户
JavaScript将JsonObject转为JosnArray_数据
控制台打印为json数组,此时已经转化为需要的形式并正确渲染为table数据,即转化成功
JavaScript将JsonObject转为JosnArray_json_02