我有一个博客,其中有一些文章。 每个用户都有一个名为“收藏夹”的道具,该道具是收藏夹帖子的数组。当用户单击每个帖子中的“心脏图标”时,该帖子的名称将被添加到用户道具中。
我想在个人资料页面上创建一个部分,用户可以在其中查看他们添加到收藏夹的帖子。为此,我必须将USER的阵列与POSTS的阵列进行匹配。
userFavoritesArray = ['post1', 'post2', 'post5', ...]
postsArray = [
{ title: 'post1',
desc: 'hello'
},
...
]
示例:
user: {
favorites: ['post1']
}
posts: [
{name: 'post1', desc: 'yo bro'},
{name: 'post2', desc: 'hello im the post2'},
{name: 'post3', desc: 'good morning'}
]
我希望为该用户提供的输出为favorites: [{name: 'post1', desc: 'yo bro'}]
答案 0 :(得分:0)
您的问题尚不完全清楚,但我猜您想要的是什么
<Grid Name="server1State" Grid.Row="1" local:CustomProperties.StarVisibility="Hidden">
<TextBlock Name="server1Star" Text="" FontFamily="{StaticResource fa-solid}" FontSize="30" Margin="10" Foreground="#375D81" Visibility="{Binding ElementName=server1State, Path=server1State.(local:CustomProperties.starVisibility)}"/>
</Grid>
在这种情况下,您可以结合使用const input = ['a', 'c']
const database = [
{ title: 'a', foo: 1 },
{ title: 'b', foo: 2 },
{ title: 'c', foo: 3 }
]
const output = mapByTitle(database, input)
console.log(output)
// Result: [ { title: 'a', foo: 1 }, { title: 'c', foo: 3 }]
(它将遍历一个数组,直到找到您的回调返回Array.prototype.find
的元素,然后返回该元素)和{{1 }}(它将遍历一个数组并返回一个新数组,其中所有元素都映射到每个元素的回调的返回值)。
例如:
true
因此,我们可以像上面那样构建上述的Array.prototype.map
函数:
// Usage of `find`:
console.log(database.find(item => item.title === 'a'))
// Result: { title: 'a', foo: 1 }
console.log(database.find(item => item.title === 'z'))
// Result: undefined
// Usage of `map`:
console.log(['hello', 'world'].map(item => item.toUpperCase() + '!'))
// Result: ['HELLO!', 'WORLD!']