循环元件在有条件的容器外面
我正在使用John Sundell的出版包,并想尝试在发布日期进行循环,并自定义输出。
尽管以主题为主题的包装,但该代码非常标准化至SWIFT。
本质上,想象一下我有一个帖子
的数组,其中包含以下数据:
let posts = [
Post( date: 2020-01-01 ),
Post( date: 2020-01-02 ),
Post( date: 2021-01-01 ),
Post( date: 2022-01-01 ),
Post( date: 2022-01-02 ),
Post( date: 2023-01-01 )
]
我想循环遍历数组,以便它输出帖子年,然后 下一个帖子是同一年不会输出的一年。如果不同,则输出新值年。
我要实现的目标:
2023 Post title 1
2022 Post title 1
Post title 2
2021 Post title 1
2020 Post title 1
Post title 2
因此,每年都与上一年打印的一年相匹配,请跳过 - 否则将其打印出来。
到目前为止,我正在使用的代码是:
func output(posts: [Posts]) -> Node { // where Node is a Publish element
var currentPostYear = 0
var savePostYear = 0
return forEach(items) { item in
currentPostYear = getItemYear(from: item.date)
if( savePostYear == currentPostYear ) {
return .h3( .text(item.title))
} else {
savePostYear = currentPostYear
return .section(
.h2( .text( "\(getItemYear(from: item.date))" ) ),
.h3( .text(item.title))
)
}
}
}
func getPostYear(from date: Date) -> Int {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy"
return Int( dateFormatter.string(from: date) ) ?? -1
}
目前是因为有条件的两个不同的返回
我有一个.h3
element 。 虽然其他人不是。
有什么方法可以编辑上述内容以使其在那一年内的所有帖子都在相同的pection
内?
代码视图(当前):
<section>
<h2>2023</h2>
<h3>Post title 1</h3>
</section>
<section>
<h2>2022</h2>
<h3>Post title 1</h3>
</section>
<h3>Post title 2</h3>
<section>
<h2>2021</h2>
<h3>Post title 1</h3>
</section>
<section>
<h2>2020</h2>
<h3>Post title 1</h3>
</section>
<h3>Post title 2</h3>
代码视图(预期):
<section>
<h2>2023</h2>
<h3>Post title 1</h3>
</section>
<section>
<h2>2022</h2>
<h3>Post title 1</h3>
<h3>Post title 2</h3>
</section>
<section>
<h2>2021</h2>
<h3>Post title 1</h3>
</section>
<section>
<h2>2020</h2>
<h3>Post title 1</h3>
<h3>Post title 2</h3>
</section>
I'm playing with John Sundell's Publish package, and want to try loop over the publish date, and customise the output.
Though Publish package themed, the code is very normalised to Swift.
In essence imagine I have an array of posts
with the following data:
let posts = [
Post( date: 2020-01-01 ),
Post( date: 2020-01-02 ),
Post( date: 2021-01-01 ),
Post( date: 2022-01-01 ),
Post( date: 2022-01-02 ),
Post( date: 2023-01-01 )
]
I want to loop over the array so that it outputs the year of the post, and then if the next post is the same year don't output the year. If it is different then output the year of the new value.
What I'm trying to achieve:
2023 Post title 1
2022 Post title 1
Post title 2
2021 Post title 1
2020 Post title 1
Post title 2
So every time the year matches the previous one printed, skip it - otherwise print it out.
The code I was working with so far is:
func output(posts: [Posts]) -> Node { // where Node is a Publish element
var currentPostYear = 0
var savePostYear = 0
return forEach(items) { item in
currentPostYear = getItemYear(from: item.date)
if( savePostYear == currentPostYear ) {
return .h3( .text(item.title))
} else {
savePostYear = currentPostYear
return .section(
.h2( .text( "\(getItemYear(from: item.date))" ) ),
.h3( .text(item.title))
)
}
}
}
func getPostYear(from date: Date) -> Int {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy"
return Int( dateFormatter.string(from: date) ) ?? -1
}
At the moment because the two different return
parts of the conditional, I have one .h3
element within the .section
while the others are not.
Is there a way I can edit the above to have it so that all the posts within that year are inside the same section
?
Code view (current):
<section>
<h2>2023</h2>
<h3>Post title 1</h3>
</section>
<section>
<h2>2022</h2>
<h3>Post title 1</h3>
</section>
<h3>Post title 2</h3>
<section>
<h2>2021</h2>
<h3>Post title 1</h3>
</section>
<section>
<h2>2020</h2>
<h3>Post title 1</h3>
</section>
<h3>Post title 2</h3>
Code view (expected):
<section>
<h2>2023</h2>
<h3>Post title 1</h3>
</section>
<section>
<h2>2022</h2>
<h3>Post title 1</h3>
<h3>Post title 2</h3>
</section>
<section>
<h2>2021</h2>
<h3>Post title 1</h3>
</section>
<section>
<h2>2020</h2>
<h3>Post title 1</h3>
<h3>Post title 2</h3>
</section>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论