Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

vue.js - How to use alphabets or roman numbers as index in Vuejs For loop?

Default iterator of for loop (v-for) in vuejs starts from 0,1,2,3... How can we set the v-for to start index with i, ii, iii, or a, b,c instead of numbers. for example this is the content:

let content = [
  "Content1",
  "Content2",
  "Content3",
  "Content4",
  "Content5",
  "Content6",
  "Content7",
  "Content8"
]

the content array contains 100+ items for different products so i don't want to manually add the roman numerals

I WANT THE OUTPUT TO LOOK LIKE THIS

i. Content1

ii. Content2

iii. Content3

iv. Content4

v. Content5

vi. Content6

vii. Content7

viii. Content8

question from:https://stackoverflow.com/questions/65932274/how-to-use-alphabets-or-roman-numbers-as-index-in-vuejs-for-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming content will be on data()

You can create a method:

toRoman(num, result = ''){
    const map = {
        M: 1000,
        CM: 900,
        D: 500,
        CD: 400,
        C: 100,
        XC: 90,
        L: 50,
        XL: 40,
        X: 10,
        IX: 9,
        V: 5,
        IV: 4,
        I: 1,
      };
      for (const key in map) {
        if (num >= map[key]) {
          if (num !== 0) {
            return this.toRoman(num - map[key], result + key);
          }
        }
      }
      return result;
    };

And access in the loop:

<div v-for="(cont, index) in content">
 {{toRoman(index).toLowerCase()}}. {{cont}}
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...