Okay, I have 2 arrays:
var state = {
users: [
{id: 1, name: 'Igor'},
{id: 2, name: 'Anton'},
{id: 3, name: 'Vasya'},
{id: 4, name: 'Pete'},
{id: 5, name: 'Dan'}
],
chats: [
{id: 1, users: [1,2], owner: 1},
{id: 2, users: [1,2,3], owner: 2},
{id: 3, users: [3,4,5], owner: 5}
]
}
I need a function that returns array like 'chats' but with actual names, not ids. So far I got this:
function loadChats() {
return state.chats.map(item =>
({
id: item.id,
users: item.users.map(user =>
state.users.find(usr =>
usr.id === user).name),
owner: item.owner
})
)
}
But I donk think that solution is good, because we dont need to map all the array, just 'users' in it. So can anybody suggest better solution? I was thinking to do smth like this:
...
state.chats.forEach(item =>
item.users.map(user =>
state.users.find(usr =>
usr.id === user)
)
)
...
But I don't like using forEach, is there any better solution?
usersandchatsshould be assigned using:not=.id(unique` and value as username.