I've seen other posts on this topic but they didn't help unfortunately. I have an array of objects. This array of objects has a visitorId, and this visitorId appears in multiple objects within the array. I want to map over this array of objects, pull all of the visitorId to be the header of a new Set array of all objects belonging to this visitorId. Some id's only have one object and some have multiple.
"events": [
{
"url": "/pages/a-big-river",
"visitorId": "d1177368-2310-11e8-9e2a-9b860a0d9039",
"timestamp": 1512754583000
},
{
"url": "/pages/a-small-dog",
"visitorId": "d1177368-2310-11e8-9e2a-9b860a0d9039",
"timestamp": 1512754631000
},
{
"url": "/pages/a-big-talk",
"visitorId": "f877b96c-9969-4abc-bbe2-54b17d030f8b",
"timestamp": 1512709065294
},
{
"url": "/pages/a-sad-story",
"visitorId": "f877b96c-9969-4abc-bbe2-54b17d030f8b",
"timestamp": 1512711000000
},
{
"url": "/pages/a-big-river",
"visitorId": "d1177368-2310-11e8-9e2a-9b860a0d9039",
"timestamp": 1512754436000
},
{
"url": "/pages/a-sad-story",
"visitorId": "f877b96c-9969-4abc-bbe2-54b17d030f8b",
"timestamp": 1512709024000
}
]
}
I have tried many things including
const map = [...new Map(
data.map(visitor => [visitor.visitorId, visitor]))];
but this only saves the last instance of a given visitor and removes all of the others.
I also tried this
const visitors = data.map(data => data.visitorId)
console.log(visitors);
const set = new Set(visitors);
const hasDuplicates = set.size < data.length;
console.log(hasDuplicates);
but it only pulls out all of the id's, and removes the rest of the data. And the id's are not grouped together.
Any help would be appreciated.