1、数组奇数在右边,偶数在左边

        var arr = [1,2,3,4,5,6];
        arr.sort(function(a){return a%2 !== 0;});
        console.log(arr);

2、查找数组第二大值

        var arr = [1,6,7,4,3,8];
        arr[arr.indexOf(Math.max.apply(null,arr))] = null;
        console.log(Math.max.apply(null,arr));

3、数组深度克隆

        var arr1 = [1,2,3,4];
        var arr2;
        arr2 = arr1.slice(0);  //或者 arr2 = arr1.concat();
        arr2[1] = 'a';
        console.log(arr2);
        console.log(arr1);

4、三维数组排序

        var arr = [8,48,3,6,9,[8,48,3,6,9,[8,48,3,6,9]]];
        var new_arr = [];
        for(var i = 0; i < arr.length; i++){       //把每个数组元素取出后,排序
            if( !arr[i].length )
                new_arr.push(arr[i]);
            else {
                for(var j = 0; j < arr[i].length; j++){
                    if( !arr[i][j].length )
                        new_arr.push(arr[i][j]);
                    else{
                        for( var k = 0; k < arr[i][j].length; k++)
                            new_arr.push(arr[i][j][k]);
                    }
                }
            }
        }
        function sortNumber(a,b){
            return a-b;
        }
        console.log(new_arr.sort(sortNumber));

kvera
19 声望7 粉丝

热爱前端的小迷妹