I have an array of contact cards that hold names & addresses etc for users. What I want to do is create another array that removes any duplicate addresses (ie people in the same household) and creates a name that is a combination of the two. For example:
{flatNum: 1, Name: "ken"},{flatNum: 1, Name: "bob"}, {flatNum: 2, Name: "emma"}
would become:
{flatNum: 1, Name: "ken & bob"}, {flatNum: 2, Name: "emma"}
I know how I can achieve this with a long for
loop type thing but was hoping to find a more concise method. I am assuming that reduce
would be the key and have been playing around. Currently got this:
let contactCardsComb = contactCards.reduce(function(a,b){
if (a.flatNum == b.flatNum){
return a.Name = a.Name+b.Name;
}
});
Which is obviously horribly wrong but any pointers would be great