对象中的数组如何转成字符串后在重新拼接??

原数据

{
    "app_name": "测试1231",
    "app_id": "3123123",
    "secret": "31213123",
    "app_role": 2,
    "scope": [
        "report_service",
        "account_service"
    ]
}

期望:

{
   "app_name": "测试1231",
    "app_id": "3123123",
    "secret": "31213123",
    "app_role": 2,
    "scope": 'report_service,account_service'
}
阅读 1.7k
2 个回答
const json = {
    "app_name": "测试1231",
    "app_id": "3123123",
    "secret": "31213123",
    "app_role": 2,
    "scope": [
        "report_service",
        "account_service"
    ]
};

json.scope = json.scope.join(',');
console.log( json );
let obj = {
    "app_name": "测试1231",
    "app_id": "3123123",
    "secret": "31213123",
    "app_role": 2,
    "scope": [
        "report_service",
        "account_service"
    ]
}

for (let key of obj) {
    if(Array.isArray(obj[key])){
        obj[key] = obj[key].join(',')
    }
}
本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题