Vueは配列内の4つのデータ項目をランダムに取得します

アイテムのスワップ機能は、4つの商品をランダムに表示します。

            /* 换一换*/
            change() {
    
    
                this.recommendItems = this.getRandomArrayElements(this.recommendlist, 4)
            },
            /* 随机获取数组中的数据*/
            getRandomArrayElements(arr, count) {
    
    
                var shuffled = arr.slice(0),
                    i = arr.length,
                    min = i - count,
                    temp, index;
                while (i-- > min) {
    
    
                    index = Math.floor((i + 1) * Math.random());
                    temp = shuffled[index];
                    shuffled[index] = shuffled[i];
                    shuffled[i] = temp;
                }
                return shuffled.slice(min);
            },

おすすめ

転載: blog.csdn.net/weixin_44433499/article/details/108448985