将json数组中多个字段组合为新数组

怎么将

[ 
    {   
        "articleId": 55,
        "title": "第一篇文章",
        "uid": 43,
        "username": "admin",
        "userAvatar": "admin.jpg"
     },
     {  
        "articleId": 56,
        "title": "第二篇文章",
        "uid": 44,
        "username": "test",
        "userAvatar": "test.jpg"
     },
]

变为

[ 
    {   
        "articleId": 55,
        "title": "第一篇文章",
        "userinfo":{
            "uid": 43,
            "username": "admin",
            "userAvatar": "admin.jpg"
        }
     },
     {  
        "articleId": 56,
        "title": "第二篇文章",
        "userinfo":{
            "uid": 44,
            "username": "test",
            "userAvatar": "test.jpg"
        }
     },
]

想要将多个字段组合为一个数组

阅读 2.1k
2 个回答
originList.map(item => {
    const { articleId, title, ...others } = item
    return {
        articleId,
        title,
        userInfo: { ...others }
    }
})
let arr = list.map(item => {
    return {
        articleId: item.articleId,
        title: item.title,
        userInfo: {
            uid: item.uid,
            userName: item.userName,
            userAvatar: item.userAvatar
        }
    }
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题