#each string in an array with blaze in Meteor

那年仲夏 提交于 2019-12-04 04:54:20

In general in Javascript, context has to be an object rather than a primitive (link). Presumably, contacts is just an array of strings, so within each div tag these strings are boxed (i.e. cast to a reference type, in this case the String object). That's what you're logging here - the String object, rather than your original primitive.

You have two options:

  1. if you use this.valueOf() it will give you the primitive string back.
  2. alternatively, consider making contacts an array of objects (like [{value: 'nameOne'}, ...]). That way, you can replace {{this}} with {{value}} and this in your event handler will give you back the object in the same format you supplied it.

I think when you use {{this}} in a template, and the underlying data is a string literal, it gets converted into a String object. For the purposes of string manipulation, you will see no difference (in fact literals get converted to objects behind the scenes when methods are called on them). However it looks weird when you write it to the console. You can just do: console.log(this.toString()); or console.log(String(this));.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!