지도 기능을 사용하여 다음 JS 코드를 어떻게 바꿀 수 있습니까?
Nov 23 2020
그래서 저는이 알고리즘에 대해 작업하고 있으며이 특정 부분에서 for ... of를 사용하고 있습니다. 어떻게 이것이 기능적으로 대체 될 수 있습니까?
const friendsOfFriendsIdsArr = [];
for (const user of graph) {
if (userFriends.includes(user.id)) {
friendsOfFriendsIdsArr.push(...user.friends);
}
}
답변
1 MikeK Nov 23 2020 at 03:19
귀하의 경우에는,
const friendsOfFriendsIdsArr = [];
for (const user of graph) {
if (userFriends.includes(user.id)) {
friendsOfFriendsIdsArr.push(...user.friends);
}
}
된다,
graph.reduce(user =>{
if (userFriends.includes(user.id)) acc.push(user.friends)
return acc
}, [])