gpt4 book ai didi

javascript - 为什么#each 标签不接受数组?

转载 作者:行者123 更新时间:2023-11-30 16:34:58 27 4
gpt4 key购买 nike

我的optionData.js如下:

"options"=
[
{
"option_text": "Just Me",
"option_image": "just-me.svg",
"option_is_selected": false
},
{
"option_text": "Spouse/Partner",
"option_image": "spouse-partner.svg",
"option_is_selected": false
},
{
"option_text": "Child/Children",
"option_image": "child-children.svg",
"option_is_selected": false
},
{
"option_text": "Grand Children",
"option_image": "grandchildren.svg",
"option_is_selected": false
},
{
"option_text": "Parent(s)",
"option_image": "parents.svg",
"option_is_selected": false
},
{
"option_text": "Pet(s)",
"option_image": "pets.svg",
"option_is_selected": false
},
{
"option_text": "Anyone Else",
"option_image": "anyone-else.svg",
"option_is_selected": false
}
]

这是我的 options.js 文件

calc_option_set1: function(){
var options_set = this.options;
option_count = options_set.length;
upperRowObjects = [];

if(option_count == 2)
{
rowBreak = -1;
}
else if(option_count % 2 == 0)
{
rowBreak = (option_count / 2);
}
else
{
rowBreak = (option_count + 1) / 2;
}

for(i=0; i<rowBreak; i++)
upperRowObjects = upperRowObjects + options_set[i];
return upperRowObjects;
},

这是 options.html

{{#each option_set1}}
<div class="col-xs-2">
<center>
<img src="/images/{{option_image}}" class="img-responsive img-rounded img-option"/>
<h5>{{option_text}}</h5>
</center>
</div>
{{/each}}

基本上我想划分选项并将它们呈现在两个不同的行 (CSS) 中。如果我打印 {{calc_option_set1}} 我得到了我想要的对象集。但是,如果我尝试像在代码中那样遍历它们,则会得到错误

"Uncaught Error: {{#each}} currently only accepts arrays, cursors or falsey values."

如何遍历方法返回的数组?有什么办法可以将它分配给一个变量然后使用它吗?请帮助...

最佳答案

数组连接不适用于 + 运算符。请改用 Array.push()。

calc_option_set1: function(){
var options_set = this.options;
option_count = options_set.length;
upperRowObjects = [];

if(option_count == 2)
{
rowBreak = -1;
}
else if(option_count % 2 == 0)
{
rowBreak = (option_count / 2);
}
else
{
rowBreak = (option_count + 1) / 2;
}

for(i=0; i<rowBreak; i++)
upperRowObjects.push(options_set[i]);
return upperRowObjects;
},

并通过以下方式访问它:

{{#each calc_option_set1}}
{{/each}}

关于javascript - 为什么#each 标签不接受数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829269/

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