1. 程式人生 > >玩一玩數組

玩一玩數組

let lse length string clas div dex else !=

1.// 搖號( 隨機抽取不重復數組 )

var arr = ‘01234‘.split(‘‘);
        var time = arr.length;
        var count = 0;
        changeArr(arr);

        function changeArr(arr) {
            // var len = arr.length;           
            // var ran = Math.floor(len * Math.random());
            // var arrOut = [];
// for (var i = 0; i < len; i++) { // if (i !== ran) { // arrOut.push(arr[i]); // } // } // console.log(arr[ran],arrOut.join(‘‘)); // count++; // if(count>=time){ // return; //
}else{ // changeArr(arrOut); // } var len = arr.length; var ran = Math.floor(len * Math.random()); var arrIndex=arr[ran]; arr.splice(ran,1); console.log(arrIndex,arr.join(‘‘)); count
++; if(count>=time){ return; }else{ changeArr(arr); } }

2.字符串 / 數組 亂序

        // var str = ‘0123456789‘;
        // str=str.split(‘‘).sort(function(){return Math.random()-0.5}).join(‘‘);
        // console.log(str);

3.刪除數組 指定項數組

var arr1 = [1, 2, 3, 4];
        var arr2 = [1, ‘3‘];

        function deleteArr(arr, arrEle) {
            if (typeof arrEle === ‘string‘ || typeof arrEle === ‘number‘) {
                var temp = [];
                temp.push(arrEle);
                arrEle = temp;
            }
            if (arrEle.length === 0) {
                return arr;
            }
            var arrOut = [];
            var flag = 0;
            for (var i = 0, j = arr.length; i < j; i++) {
                flag = 0;
                for (var m = 0, n = arrEle.length; m < n; m++) {
                    if (arr[i] !== arrEle[m]) {
                        flag++;
                    }
                }
                if (flag === n) {
                    arrOut.push(arr[i]);
                }
            }
            return arrOut;
        }
        console.log(deleteArr(arr1, arr2));

玩一玩數組