I have an array like this (1st level nesting is intended)
const names = [
[{
name: "John",
country: "USA"
}, {
name: "Mary",
country: "UK"
}], {
name: "Paul",
country: "USA"
}
]
I would like to convert this into
const groupBy = [{
country: "USA",
names: [{
name: "John"
}, {
name: "Paul"
}]
},
{
country: "UK",
names: [{
name: "Mary"
}]
}
]
I am wondering how I could use reduce() or another method to achieve the desired outcomes?