久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Mustache.js 中數組元素的索引

Index of an array element in Mustache.js(Mustache.js 中數組元素的索引)
本文介紹了Mustache.js 中數組元素的索引的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是我想在 Mustache.js 中做的,但沒有看到文檔如何做.

var view = {items:['Mercury','Venus','Earth','Mars']};var template = "<ul> {{#items}}<li>{{i}} - {{.}}</li>{{/items}} </ul>";var html = Mustache.to_html(模板,視圖);

期望的輸出:

    <li>0 - 水星</li><li>1 - 金星</li><li>2 - 地球</li><li>3 - 火星</li></ul>

解決方案

另一種解決方案,不用胡亂用 Mustache.js

與其胡亂胡鬧,不如使用 <ol></ol> 而不是 <ul></ul>,這將為每個項目添加前綴 index+1.

如果您愿意,可以使用 css 將起始數字更改為 0,它會完全按照您的意愿呈現.您甚至可以將數字后面的 dot 更改為諸如 " 之類的內容.- ",問題就解決了.

    <li>水星</li><li>金星</li><li>地球</li><li>火星</li></ol>

以上將呈現為:

<代碼>1.汞2. 金星3.地球4.火星


Mustache.js 方法

如果您想在 mustache 中執行此操作,正確的方法是將字符串數組轉換為對象數組,其中存在索引和字符串值.

//這是我從腦后寫的,它未經測試且無法保證//無需修改即可工作,盡管其背后的理論是有效的.var array = [123",stackoverflow",abc"];var obj_array = [];對于(數組中的 idx)obj_array.push ({'index': idx, 'str': array[idx]});var view = {items: obj_array};var template = "<ul>{{#items}}<li>{{index}} - {{str}}</li>{{/items}}</ul>";var html = Mustache.to_html(模板,視圖);

This is what I'd like to do in Mustache.js but not seeing how with the documentation.

var view = {items:['Mercury','Venus','Earth','Mars']};
var template = "<ul> {{#items}}<li>{{i}} - {{.}}</li>{{/items}} </ul>";
var html = Mustache.to_html(template,view);

Desired output:

<ul>
  <li>0 - Mercury</li>
  <li>1 - Venus</li>
  <li>2 - Earth</li>
  <li>3 - Mars</li>
</ul>

解決方案

An alternative solution, without fooling around with Mustache.js

Instead of fooling around with mustache you might as well use a <ol></ol> instead of <ul></ul>, that will prefix each item with index+1.

If you'd like you can use css to change the starting number to 0, and it will render exactly as you want. You can even change the dot after the number, to something such as " - ", and problem is solved.

<ol>
  <li>Mercury</li>
  <li>Venus</li>
  <li>Earth</li>
  <li>Mars</li>
</ol>

the above will render as:

1. Mercury
2. Venus
3. Earth
4. Mars


the Mustache.js way to do it

The proper method if you'd like to do it in mustache is to convert your array of strings to an array of objects, where index and string value is present.

// I wrote this from the back of my head, it's untested and not guaranteed
// to work without modifications, though the theory behind it is valid.


var array     = ["123","stackoverflow","abc"];
var obj_array = [];

for (idx in array)
   obj_array.push ({'index': idx, 'str': array[idx]});

var view     = {items: obj_array};
var template = "<ul>{{#items}}<li>{{index}} - {{str}}</li>{{/items}}</ul>";
var html     = Mustache.to_html(template,view);

這篇關于Mustache.js 中數組元素的索引的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 亚洲第一区国产精品 | 久久久久久久国产 | 2018天天干天天操 | 国产jizz女人多喷水99 | 国产精品成av人在线视午夜片 | 亚洲444kkkk在线观看最新 | 国产欧美日韩综合精品一区二区 | 亚洲第一网站 | 久久五月婷 | 亚洲精品99999 | 1级毛片| 国产精品一区二区免费 | 亚洲成人毛片 | 日日干夜夜操 | 亚洲精品中文在线观看 | 亚洲欧美国产毛片在线 | 亚洲视频欧美视频 | 亚洲中午字幕 | 精品国产一区二区在线 | 久久国产美女视频 | 亚州春色 | 一区欧美 | 97日韩精品 | 97精品国产97久久久久久免费 | 精品欧美一区二区在线观看欧美熟 | 五月婷婷丁香 | 日韩有码在线播放 | 免费看黄色视屏 | 成人午夜影院 | 一区二区成人 | 久久亚洲精品国产精品紫薇 | 精品欧美激情在线观看 | 黄色av网站在线观看 | 免费视频一区 | 日韩综合网| 国产在线视频一区二区 | 国产一区不卡在线观看 | 国产日韩久久 | 在线色网 | 欧美一区二区三区在线 | 午夜网站视频 |