0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

vue.js 配列をランダムに取得(シャッフル、カット)

Posted at

vue.js 、 javascript側で
配列をシャッフルしてカットしたい
こんな感じ。

hoge.vue

<script>

    export default {

        data() {
            return {

            }
        },

        methods: {

            getIkemen(){

                let dataform = new FormData();
                axios.post('/user/show/', dataform).then(e => {

                    //データをシャッフル
                    this.ikemenList = this.shuffle(e.data.res.data);
                    //配列をカットし、データを3つ取得
                    this.ikemenList = this.ikemenList.slice(0,3);

                }).catch((error) => {
                    console.log(" イケメン取得 エラー");
                });


            },

            shuffle(array) {
                for (let i = array.length - 1; i > 0; i--) {
                    let r = Math.floor(Math.random() * (i + 1))
                    let tmp = array[i]
                    array[i] = array[r]
                    array[r] = tmp
                }
                return array;
            },


        }
    };
</script>


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?