Handlebars:如何为sum数组制作自定义助手?

时间:2018-05-15 12:47:54

标签: handlebars.js

我试着这样:

sum: function(arr) {
    const s = 0;
    for(const i=0; i<arr.length; i++) {
      s = s + arr[i];
    }
    return s;
}

并在表中输出如下:

{{#each something}}
  <td>{{sum this}}</td>
{{/each}}

但在这种情况下没有出现任何建议?

[this]数组包含数字

50 | 10 | 10 | 10 | 5  | 5 |
60 | 10 | 10 | 10 | 5  | 5 |
50 | 10 | 10 | 10 | 10 | 10|

所以结果应该是

160 | 30 | 30 | 30 | 20 | 20 |

但在我的方法中返回零值。

如果我像@Christophe Thiry那样告诉我:

sum: function(item) {

  result = item[0]
  for (i=0; i<result.length; i++) {
    for (j=1; j<item.length; j++) {
      result[i] = result[i]+item[j][i];
    }
  }

  return result.toString();
}

而且只是:

<td>{{sum this}}</td>

我得 TypeError:无法读取未定义的属性'length'

架构模型:

const StateResultSchema = new Schema({

    electoralUnit: {
        type: Schema.Types.ObjectId,
        ref: 'ElectoralUnit',
        required: true,
        unique: true
    },

    allVotes: {
        type: Number,
        required: true
    },

    validVotes: {
        type: Number,
        required: true
    },

    invalidVotes: {
        type: Number,
        required: true
    },

    partyVotes: {
        type: [Number],
        required: true
    }

});

这是它在MongoDB Compass中的样子“

_id:ObjectId("5ac4e01d46fa2b21280bd981")
electoralUnit:ObjectId("5ab906612f30fe23dc592591")
allVotes:100
validVotes:90
invalidVotes:10
partyVotes:[50,10,10,10,5,5]
__v:0

1 个答案:

答案 0 :(得分:0)

你不需要每个助手,你必须使用你直接创建的助手{{sum something}}并且还要检查你的助手,因为你只迭代一个数组。 请查看以下代码段,了解如何实现这一目标。

&#13;
&#13;
$(document).ready(function () {
  var context = { "something" : [
  { "_id":"5ac4e01d46fa2b21280bd981",  "electoralUnit":"5ab906612f30fe23dc592591",  "allVotes":100,  "validVotes":90,  "invalidVotes":10,  "partyVotes":[50,10,10,10,5,5],  "__v":0},
  { "_id":"5ac4e01d46fa2b21280bd982",  "electoralUnit":"5ab906612f30fe23dc592592",  "allVotes":100,  "validVotes":90,  "invalidVotes":10,  "partyVotes":[50,10,10,10,5,5],  "__v":0},
  { "_id":"5ac4e01d46fa2b21280bd983",  "electoralUnit":"5ab906612f30fe23dc592593",  "allVotes":100,  "validVotes":90,  "invalidVotes":10,  "partyVotes":[60,10,10,10,5,5 ],  "__v":0},
  { "_id":"5ac4e01d46fa2b21280bd984",  "electoralUnit":"5ab906612f30fe23dc592594",  "allVotes":100,  "validVotes":90,  "invalidVotes":10,  "partyVotes":[50,10,10,10,10,10],  "__v":0}
      ]
  };
  Handlebars.registerHelper('sum', function(item) {
  result = item[0].partyVotes;
  for (i=0; i<result.length; i++) {
    for (j=1; j<item.length; j++) {
      result[i] = result[i]+item[j].partyVotes[i];
    }
  }
  
  return result.toString();
   });
	var source   = $("#sourceTemplate").html();
  var template = Handlebars.compile(source);
  var html    = template(context);
  $("#resultPlaceholder").html(html);
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script id="sourceTemplate" type="text/x-handlebars-template">
{{sum something}}
</script>
<br/>
<div id="resultPlaceholder">
</div>
&#13;
&#13;
&#13;