如何为对象添加值并删除当前值?

How to add value to object and remove current value?

我有这样的数据:

users = [{
    "emp_id": 1,
    "user": {
        "emp_full_name": "Test",
        "emp_email": "test@gmail.com",
        "emp_phone_no": null,
        "preferred_work_type": null
    },
    "hashtag": {
        "id": 1,
        "name": "NodeJs",
        "hashtag_group_id": 1
    },
    "difficulty": "HARD"
}, {
    "emp_id": 2,
    "user": {
        "emp_full_name": "test2",
        "emp_email": "test2@gmail.com",
        "emp_phone_no": null,
        "preferred_work_type": null
    },
    "hashtag": {
        "id": 1,
        "name": "NodeJs",
        "hashtag_group_id": 1
    },
    "difficulty": "EASY"
}, {
    "emp_id": 1,
    "user": {
        "emp_full_name": "Test",
        "emp_email": "test@gmail.com",
        "emp_phone_no": null,
        "preferred_work_type": null
    },
    "hashtag": {
        "id": 4,
        "name": "Javascript",
        "hashtag_group_id": 1
    },
    "difficulty": "HARD"
}]

我想将 hashtag 添加到具有相同 emp_id 的对象中。如果 emp_id 有多个数据,则应删除包含 emp_id 和单个 hashtag 数据的数据。

所以基本上这就是我的预期:

[{
    "emp_id": 1,
    "user": {
        "emp_full_name": "Test",
        "emp_email": "test@gmail.com",
        "emp_phone_no": null,
        "preferred_work_type": null
    },
    "hashtag": [{
        "id": 1,
        "name": "NodeJs",
        "hashtag_group_id": 1
    }, {
        "id": 4,
        "name": "Javascript",
        "hashtag_group_id": 1
    }],
    "difficulty": "HARD"
}, {
    "emp_id": 2,
    "user": {
        "emp_full_name": "test2",
        "emp_email": "test2@gmail.com",
        "emp_phone_no": null,
        "preferred_work_type": null
    },
    "hashtag": {
        "id": 1,
        "name": "NodeJs",
        "hashtag_group_id": 1
    },
    "difficulty": "EASY"
}]

如何这样转换数据?

我不知道如何解决这个问题,我尝试使用 filter()map() 以及一些验证条件,但无法正常工作。

您可以创建一个由 emp_id 键控的地图并通过该键收集用户。当已有条目时,使用 [].concat 扩展 hashtag。如果它还不是数组,这将创建一个数组。

const users = [{"emp_id": 1,"user": {"emp_full_name": "Test","emp_email": "test@gmail.com","emp_phone_no": null,"preferred_work_type": null},"hashtag": {"id": 1,"name": "NodeJs","hashtag_group_id": 1},"difficulty": "HARD"},{"emp_id": 2,"user": {"emp_full_name": "test2","emp_email": "test2@gmail.com","emp_phone_no": null,"preferred_work_type": null},"hashtag": {"id": 1,"name": "NodeJs","hashtag_group_id": 1},"difficulty": "EASY"},{"emp_id": 1,"user": {"emp_full_name": "Test","emp_email": "test@gmail.com","emp_phone_no": null,"preferred_work_type": null},"hashtag": {"id": 4,"name": "Javascript","hashtag_group_id": 1},"difficulty": "HARD"}];

const map = new Map;
for (const user of users) {
    const match = map.get(user.emp_id);
    if (match) match.hashtag = [].concat(match.hashtag, user.hashtag);
    else map.set(user.emp_id, {...user});
}
const result = [...map.values()];

console.log(result);

您可以将 .reduce().findIndex() 一起使用。试试这个

let users = '[{"emp_id": 1, "user": {"emp_full_name": "Test", "emp_email": "test@gmail.com", "emp_phone_no": null, "preferred_work_type": null }, "hashtag": {"id": 1, "name": "NodeJs", "hashtag_group_id": 1 }, "difficulty": "HARD"}, {"emp_id": 2, "user": {"emp_full_name": "test2", "emp_email": "test2@gmail.com", "emp_phone_no": null, "preferred_work_type": null }, "hashtag": {"id": 1, "name": "NodeJs", "hashtag_group_id": 1 }, "difficulty": "EASY"}, {"emp_id": 1, "user": {"emp_full_name": "Test", "emp_email": "test@gmail.com", "emp_phone_no": null, "preferred_work_type": null }, "hashtag": {"id": 4, "name": "Javascript", "hashtag_group_id": 1 }, "difficulty": "HARD"} ]';
users = JSON.parse(users)

users = users.reduce((arr, o) => {
    let idx = arr.findIndex(({emp_id}) => emp_id === o.emp_id);
    if( idx !== -1 ) arr[idx].hashtag = [].concat(arr[idx].hashtag, o.hashtag)
    else arr.push(o)

    return arr;
}, []);

console.log(users)