Home > Blockchain >  How to add value to object and remove current value?
How to add value to object and remove current value?

Time:05-18

I have data like this :

users = [{
    "emp_id": 1,
    "user": {
        "emp_full_name": "Test",
        "emp_email": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "emp_phone_no": null,
        "preferred_work_type": null
    },
    "hashtag": {
        "id": 4,
        "name": "Javascript",
        "hashtag_group_id": 1
    },
    "difficulty": "HARD"
}]

I want to add hashtag to same the object that has the same emp_id. If emp_id has more than one data then the data that has the emp_id with the single hashtag data should be removed.

So basically this is what I expected:

[{
    "emp_id": 1,
    "user": {
        "emp_full_name": "Test",
        "emp_email": "[email protected]",
        "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": "[email protected]",
        "emp_phone_no": null,
        "preferred_work_type": null
    },
    "hashtag": {
        "id": 1,
        "name": "NodeJs",
        "hashtag_group_id": 1
    },
    "difficulty": "EASY"
}]

How to transform the data like that?

I have no idea how to solve that, I tried using filter(), and map() with some validation condition, but couldn't get it to work.

CodePudding user response:

You could create a Map keyed by emp_id and collect the users by that key. When there is already an entry, extend the hashtag using [].concat. This will create an array if it wasn't an array yet.

const users = [{"emp_id": 1,"user": {"emp_full_name": "Test","emp_email": "[email protected]","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": "[email protected]","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": "[email protected]","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);

CodePudding user response:

You can use .reduce() with .findIndex(). Try this

let users = [{"emp_id": 1, "user": {"emp_full_name": "Test", "emp_email": "[email protected]", "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": "[email protected]", "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": "[email protected]", "emp_phone_no": null, "preferred_work_type": null }, "hashtag": {"id": 4, "name": "Javascript", "hashtag_group_id": 1 }, "difficulty": "HARD"} ];

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)

  • Related