Math.floor(Math.random() * array.length),splice

1、Math.floor(Math.random() * array.length) 

返回长度内的索引

eg:

changeLimit () {
  function getArrayItems(arr, num) {
    const temp_array = [];
    for(let index in arr) {
      temp_array.push(arr[index]);
    }
    const return_array = [];
    for (let i = 0; i<num; i++) {
      if(temp_array.length>0) {
        const arrIndex = Math.floor(Math.random()*temp_array.length);
        return_array[i] = temp_array[arrIndex];
        temp_array.splice(arrIndex, 1);
      } else {
        break;
      }
    }
    return return_array;
  }
  this.randomMovieList = getArrayItems(this.movieList, 5);
}

2、splice

temp_array.splice(arrIndex, 1),

删除temp_array中下标为arrIndex的那个元素

posted @ 2019-11-19 10:55  莯汐  阅读(1207)  评论(0编辑  收藏  举报