Home > Web Front-end > Front-end Q&A > How to reverse an array in vue

How to reverse an array in vue

青灯夜游
Release: 2022-01-10 16:07:05
Original
4327 people have browsed it

In vue, you can use the "v-for" instruction and calculated properties to reverse the array, the syntax "

" and "computed:{reverseDIV(){ return this.items.reverse()}}".

How to reverse an array in vue

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

Vue's method of reversing an array

Method 1:

<template>
    <div>
        <div v-for="item in Array.prototype.reverse.call(items)">
            <li>{{item}}</li>
        </div>
    </div>
</template>
 
<script>
    export default {
        name: "List",
        data(){
            return{
                items:[1,2,3,4]
            }
        },
 
    }
</script>
Copy after login

Method Two (computed attribute):

<template>
    <div>
        <div v-for="item in reverseDIV">
            <li>{{item}}</li>
        </div>
    </div>
</template>
 
<script>
    export default {
        name: "List",
        data() {
            return {
                items: [1, 2, 3, 4]
            }
        },
        computed: {
            reverseDIV() {
                return this.items.reverse()
            }
        }
    }
</script>
Copy after login

Description: Computed attribute

Type: { [key: string]: Function | { get: Function , set: Function } }

Details:

Computed properties will be mixed into the Vue instance. The this context of all getters and setters is automatically bound to the Vue instance.

Note that if you use an arrow function for a computed property, this will not point to the instance of the component, but you can still access its instance as the first parameter of the function.

computed: {
  aDouble: vm => vm.a * 2
}
Copy after login

The results of calculated properties will be cached and will not be recalculated unless the dependent responsive properties change. Note that if a dependency (such as a non-reactive property) is outside the scope of the instance, the computed property will not be updated.

Mainly a series of operations we perform without polluting the source data

[Related recommendations: vue.js tutorial]

The above is the detailed content of How to reverse an array in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template