I attempted to use import _ from 'lodash-es'
and
_.union(user.users.map(user => user.city))
. However, the result was not as expected, such as: ["city_id1", "city_id2", "city_id3", "city_id4"]
What is the correct way to utilize _.union
, or is there a better method to flatten all arrays and create a unique array?
### data
const user =
{
"users": [
{
"id": "user_id1",
"city": ["city_id1", "city_id2"]
},
{
"id": "user_id2",
"city": ["city_id3", "city_id4"]
},
{
"id": "user_id3",
"city": ["city_id1", "city_id3"]
},
{
"id": "user_id4",
"city": ["city_id2", "city_id4"]
}
]
}