Xử lý mảng - nhóm 2 thuộc tính thành object

Mình có một mảng thế này:

var data = [{
    name: 'Margret Swanson',
    id: 'ms',
    color: 'red',
    label_color: 'white',
    pos: 'President', 
    phone: '630-555-1111'
},
{
    name: 'Mark Hudson',
    id: 'mh',
    parent: 'ms',
    pos: 'Vice President Marketing',
    phone: '630-555-1111'
}]

Giờ mình muốn trộn thuộc tính pos và phone thành attributes:

[
        {
          name: 'Margret Swanson',
          id: 'ms',
          color: 'red',
          label_color: 'white',
          attributes: {pos: 'President',phone: '630-555-1111' }
        },
        {
          name: 'Mark Hudson',
          id: 'mh',
          parent: 'ms',
          attributes: {
            pos: 'Vice President Marketing',
            phone: '630-555-1111'
          }
]

Các bạn giúp mình với ạ :grinning:

Dùng tính năng map array của es6 là xong

6 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?