美文网首页
2019-09-02

2019-09-02

作者: Amuer蘃 | 来源:发表于2019-10-11 11:32 被阅读0次

    求数组的随机下标

    var a =['a','b','b','d','e']
             document.write(
             (Math.floor(Math.random() * (Max - Min + 1)+Min))
             //数组下标最大值-最小值+1+最小值    //公式
             Math.floor(Math.random()*(a.length))
             )
             document.write(
             a[Math.floor(Math.random()*(a.length))]
             )
    

    数组去重

    var arr = [1, 7, 2, 5, 8, 8, 6, 5, 5, 4, 5, 3]
          function noRepeat(brr) {
              var newarr = [] //接受去重后的数组
              for (var i = 0; i < brr.length; i++) {//设定i从零开始,并小于数组的长度的数
                  if (newarr.indexOf(brr[i]) == -1) {//判断新的数组里是否已经有了这个数
                      newarr.push(brr[i])//如果没有,将这个数放进新的数组
                  }
              }
              return newarr;
          }
          console.log(noRepeat(arr))
    

    相关文章

      网友评论

          本文标题:2019-09-02

          本文链接:https://www.haomeiwen.com/subject/zwupyctx.html