【发布时间】:2023-02-17 19:37:09
【问题描述】:
我搜索了其他帖子,但找不到我要找的东西。我正在循环访问来自 sanity api 的数据,但我想将其限制为仅返回前 4 个项目。实现这一目标的最佳方法是什么?这是有问题的代码
{mappedPosts && mappedPosts && mappedPosts.map ( (post) => (
<Col className="mapped-posts" style={redLine} xs="12" lg="4">
<div style={imageContainerStyle} onClick={() => router.push(`/logos/${post.slug.current}`)} key={post.index} >
<img
src={post.mainImage}
alt={post.title}
style={imgStyle}
layout="fill" />
<h3 style={titleStyle}>{post.title}</h3>
</div>
</Col>
)) }
我可以在循环中做一些事情吗:
{mappedPosts && mappedPosts.length < 4 && mappedPosts.map ( (post) => (
或者有没有更好的方法来控制 map 函数返回多少数据?
【问题讨论】:
-
为什么不只获取前 4 个然后映射它们呢?
标签: javascript loops map-function