ak8*_*k85 0 javascript arrays json static-site nunjucks
在 javascript 中,我可以遍历一个数组来输出我的对象,如下所示。
var myArr = ["one","two","three"]
var myObj = {
"one": {
"title": "ones",
"desc": "this is one"
},
"three": {
"title": "threes",
"desc": "this is three"
},
"two": {
"title": "twos",
"desc": "this is two"
}
}
myArr.forEach(function (value) {
console.log(myObj[value].title, myObj[value].desc);
});
Run Code Online (Sandbox Code Playgroud)
产出
ones this is one
twos this is two
threes this is three
console.log("number 2 - ",myObj[myArr[1]].desc)
console.log("number 2 - ",myObj["two"].desc)
Run Code Online (Sandbox Code Playgroud)
产出
number 2 - this is two
number 2 - this is two
我希望能够在 nunjucks 中做到这一点,因为我想控制显示顺序,myArr但也希望能够灵活地拥有一个页面,例如 one.html,我可以在其中专门针对一个页面,例如{{testObj.one.title}}.
修女如何做到这一点?我已经尝试了以下。
-- nunjucks --
<ul>
{% for item in testArr %}
<li>{{item}} - {{testObj.one.title}}</li>
{% endfor %}
</ul>
<ul>
{% for obj, item in testObj %}
<li>{{ obj }}: {{ item | dump }} - {{ item.title }} - {{ item.desc }}</li>
{% endfor %}
</ul>
--- output ---
<ul>
<li>one - ones</li>
<li>two - ones</li>
<li>three - ones</li>
</ul>
<ul>
<li>one: {"title":"ones","desc":"this is one"} - ones - this is one</li>
<li>three: {"title":"threes","desc":"this is three"} - threes - this is three</li>
<li>two: {"title":"twos","desc":"this is two"} - twos - this is two</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我的理想输出去,会象下面,我可以根据我的订单显示myArr,但每个项目的展现自己关键的内容myObj
<ul>
<li>one - ones</li>
<li>two - twos</li>
<li>three - threes</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
<ul>
{% for item in myArr %}
<li>{{item}} - {{myObj[item].title}}</li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
?
| 归档时间: |
|
| 查看次数: |
6025 次 |
| 最近记录: |