Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can get index & count in vuejs

Tags:

vue.js

I have code like this (JSFiddle)

  <li v-for="(itemObjKey, catalog) in catalogs">this index : {{itemObjKey}}</li> 

Output:
this index: 0
this index: 1

My question is:

  1. How can I get value index first begin: 1 for example
    I want output like this:
    this index: 1
    this index: 2

  2. How can I get count from index, i.e. output like this:
    this index: 1
    this index: 2
    this count: 2 field
like image 814
b4dQuetions Avatar asked Mar 07 '16 09:03

b4dQuetions


People also ask

How do you find index?

To see the index for a specific table use SHOW INDEX: SHOW INDEX FROM yourtable; To see indexes for all tables within a specific schema you can use the STATISTICS table from INFORMATION_SCHEMA: SELECT DISTINCT TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.

How do you find the index of an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.

What is an index in array?

The index indicates the position of the element within the array (starting from 1) and is either a number or a field containing a number.


2 Answers

you can just add 1

<li v-for="(catalog, itemObjKey) in catalogs">this index : {{itemObjKey + 1}}</li> 

to get the length of an array/objects

{{ catalogs.length }} 
like image 96
jay temp Avatar answered Sep 20 '22 18:09

jay temp


In case, your data is in the following structure, you get string as an index

items = {    am:"Amharic",    ar:"Arabic",    az:"Azerbaijani",    ba:"Bashkir",    be:"Belarusian" } 

In this case, you can use extra variable to get the index in number:

<ul>   <li v-for="(item, key, index) in items">     {{ item }} - {{ key }} - {{ index }}   </li> </ul> 

Source: https://alligator.io/vuejs/iterating-v-for/

like image 30
nsv Avatar answered Sep 20 '22 18:09

nsv