gpt4 book ai didi

arrays - 循环遍历 Twig 中的两个数组

转载 作者:行者123 更新时间:2023-12-04 07:57:29 25 4
gpt4 key购买 nike

我不知道您如何称呼以下内容,但我的循环中需要一种模式。
假设我有 2 个数组。

Array1 = 1..10

Array2 = ['a','b','a','b','b']
我需要的结果应该是:
1a,
2b,
3a,
4b,
5b,
6a,
7b,
8a,
9b,
10b
如何使用 Twig 模板实现这一目标?

最佳答案

使用 moduloJavaScript :

const _getArr = (from, to, chars) => {
const res = [];
const len = chars.length;
let count = 0;
for (let i = from; i <= to; i++) {
console.log(count, count%len, chars[count % len]);
res[count] = `${i}${chars[count % len]}`;
count++;
}
return res;
}

console.log( _getArr(1, 10, ['a','b','a','b','b']) );

关于arrays - 循环遍历 Twig 中的两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66625714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com