Javascript 如何使用手柄输出JSON对象中数组的每个元素

Javascript 如何使用手柄输出JSON对象中数组的每个元素,javascript,html,handlebars.js,Javascript,Html,Handlebars.js,我试图在输出中输出以下数组的内容 JSON对象: let output = { out:[ ] }; 我通过执行以下操作在数组中添加内容: for (let j = 0; j < length ; j++){ output.out[j]= "stuff" + j; } 我没有收到任何错误,但没有输出任何内容。 对不起,我的英语很笨拙。请确保必须将对象或数据传递给模板 模板: <script id="example-template" type="text/x-ha

我试图在输出中输出以下数组的内容 JSON对象:

let output = {
    out:[
    ]
};
我通过执行以下操作在数组中添加内容:

for (let j = 0; j < length ; j++){
output.out[j]= "stuff" + j;
}
我没有收到任何错误,但没有输出任何内容。
对不起,我的英语很笨拙。

请确保必须将对象或数据传递给模板

模板:

<script id="example-template" type="text/x-handlebars-template"> 
    {{#each out}}
        {{this}}
        <br>
    {{/each}}
</script>

{{{#各出}
{{this}}

{{/每个}}
JavaScript:

var source = $("#example-template").html(); 
var template = Handlebars.compile(source); 
var output = {
    out:[
    ]
};
for (let j = 0; j < 10 ; j++){
     output.out[j]= "stuff" + j;
}
$('body').append(template(output));
var source=$(“#示例模板”).html();
var template=handlebar.compile(源代码);
变量输出={
输出:[
]
};
for(设j=0;j<10;j++){
output.out[j]=“stuff”+j;
}
$('body').append(模板(输出));

答案只是宣布和定义一切正确。 我只是没有在JSON对象中定义out[]数组,而是在函数中定义了如下过程:

let output = {
};

function do(){
  output.out = [];
    for (let j = 0; j < length ; j++){
      output.out[j]= "stuff" + j;
    }
}
let输出={
};
函数do(){
output.out=[];
for(设j=0;j
我将output.out替换为把手部分的output.out。它起作用了。
我明白,在我的情况下,对象被重新写入,并把一切都搞砸了

什么是
stuff
?字符串中的stuff。为了更精确,我要编辑它为什么要使用output.out?output是传递给模板的变量的名称,与数据无关。从以下答案中查看JSFIDLE:@IgnacyDebicki谢谢兄弟,我正在查看。编辑:我尝试将output.out替换为just out,但它仍然没有输出任何内容
var source = $("#example-template").html(); 
var template = Handlebars.compile(source); 
var output = {
    out:[
    ]
};
for (let j = 0; j < 10 ; j++){
     output.out[j]= "stuff" + j;
}
$('body').append(template(output));
let output = {
};

function do(){
  output.out = [];
    for (let j = 0; j < length ; j++){
      output.out[j]= "stuff" + j;
    }
}