I have an array of objects similar to
const array = [
{
documentId: "document1",
writers: [
{
name: "writer1",
date: "2017-12-11"
},
{
name: "writer2",
date: "2017-12-11"
}
]
},
{
documentId: "document2",
writers: [
{
name: "writer1",
date: "2017-12-11"
},
{
name: "writer3",
date: "2017-12-11"
}
]
},
{
documentId: "document3",
writers: [
{
name: "writer3",
date: "2017-12-11"
},
{
name: "writer4",
date: "2017-12-11"
}
]
},
I'm trying to extract all the unique writer's names and match them to all the documents they have written so that the final array looks something like this:
const finalArray = [
{name: "writter1", documents: ["document1", "document2"]},
{name: "writter2", documents: ["document1"]},
{name: "writter3", documents: ["document2", "document3"]},
{name: "writter4", documents: ["document3"]}
]