循环元件在有条件的容器外面

发布于 2025-01-27 10:56:57 字数 2623 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文