#5 遍历Hash对象
2017-07-01 13:18
274 查看
英文原版:https://guides.emberjs.com/v2.13.0/templates/displaying-the-keys-in-an-object/
如果你需要在模板中显示javascript对象的key或者value,那么你可以使用{{#each-in}}助手:
{{#each-in}}会按照key来进行遍历。其中 category参数就是对象中的key,products参数是key对应的value。
上面的例子最终会得到下面的结果:
本节完
如果你需要在模板中显示javascript对象的key或者value,那么你可以使用{{#each-in}}助手:
/app/components/store-categories.js import Ember from 'ember'; export default Ember.Component.extend({ willRender() { // Set the "categories" property to a JavaScript object // with the category name as the key and the value a list // of products. this.set('categories', { 'Bourbons': ['Bulleit', 'Four Roses', 'Woodford Reserve'], 'Ryes': ['WhistlePig', 'High West'] }); } });
/app/templates/components/store-categories.hbs <ul> {{#each-in categories as |category products|}} <li>{{category}} <ol> {{#each products as |product|}} <li>{{product}}</li> {{/each}} </ol> </li> {{/each-in}} </ul>
{{#each-in}}会按照key来进行遍历。其中 category参数就是对象中的key,products参数是key对应的value。
上面的例子最终会得到下面的结果:
<ul> <li>Bourbons <ol> <li>Bulleit</li> <li>Four Roses</li> <li>Woodford Reserve</li> </ol> </li> <li>Ryes <ol> <li>WhistlePig</li> <li>High West</li> </ol> </li> </ul>
排序
上面例子中数据展示的顺序与定义时的顺序是一样的。但是,如果你想要人为的对它进行排序,那么你需要利用key来构建一个数组,并且通过javascript的方法,如:sort()来对此数组进行排序,然后使用{{#each}}助手来输出。空集合
{{#each-in}}助手同样也带有{{else}},如果被遍历的对象为空,null或者undefined。那么就会去渲染{{else}}块中的内容:{{#each-in people as |name person|}} Hello, {{name}}! You are {{person.age}} years old. {{else}} Sorry, nobody is here. {{/each-in}}
本节完
相关文章推荐
- Java基础知识强化之集合框架笔记41:Set集合之HashSet存储自定义对象并遍历练习
- Java基础知识强化之集合框架笔记40:Set集合之HashSet存储自定义对象并遍历
- kettle利用参数遍历执行指定目录下的所有对象
- python面对对象编程------4:类基本的特殊方法__str__,__repr__,__hash__,__new__,__bool__,6大比较方法
- jQuery遍历对象/数组/集合
- 在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法。两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点。 $().each,对于这个方法,在
- 能用foreach遍历访问的对象需要实现____接口或声明____方法的类型
- Asp遍历服务器对象的代码
- $.each()遍历数组和集合对象
- foreach遍历自定义数据对象在各个浏览器下的顺序差异
- IOS遍历未知对象属性、函数
- PHP之对象遍历
- Java中遍历Map对象的4种方法
- JS 对象遍历
- 过滤数组中重复的值 及 对象、数组遍历总结
- 集合框架_ArrayList存储自定义对象并遍历
- 关于AS3的for in遍历Object对象的属性问题
- QTP描述性编程-遍历页面多个对象
- 使用list集合遍历对象
- 贴一段遍历memcached缓存对象的小脚本