Skip to content

nth

用 nth 模式语法从数组中选取位置。

语法

knap
{{ value | nth:3 }}
{{ value | nth:2n }}
{{ value | nth:n+3 }}
{{ value | nth:2,3:4 }}

用法

  • 索引从 1 开始。模式支持单个位置、每隔 n 项、带偏移量,或重复分组内的位置。

示例

每隔 n 项

Data
json
{
  "items": [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
  ]
}
Template
knap
{{ items | nth:2n }}
Output
md
["b","d","f"]

重复分组

Data
json
{
  "items": [
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8
  ]
}
Template
knap
{{ items | nth:2,3:4 }}
Output
md
[2,3,6,7]