I have an array with 6 elements. Each elements has 3 properties: id, value, and friends.
In my code I manipulate the array by adding new values to certain elements' friends property, but in some circumstances this leads to duplicate values in the friends property. For instance, in myArray below, element 5 cannot be friends with element 1 twice -- they're either friends or not.
const myArray = [
{"id": 1, "value": 75, "friends": 3},
{"id": 2, "value": 40, "friends": 4},
{"id": 3, "value": 60, "friends": 5},
{"id": 4, "value": 62, "friends": [6, 1]},
{"id": 5, "value": 55, "friends": [1, 1]},
{"id": 6, "value": 33, "friends": [2, 1]}
];
How can I remove duplicate values in the friends property in myArray?
My desired output would look like this:
const myArray = [
{"id": 1, "value": 75, "friends": 3},
{"id": 2, "value": 40, "friends": 4},
{"id": 3, "value": 60, "friends": 5},
{"id": 4, "value": 62, "friends": [6, 1]},
{"id": 5, "value": 55, "friends": 1},
{"id": 6, "value": 33, "friends": [2, 1]}
];
I'm new to javascript and not sure where to start with this. My intuition was to first get all the friends and then apply some filtering function:
const friendList = myArray.map(getFriends);
var uniqueFriends = [...new Set(friendList)];
But this clearly doesn't work.