1

array.push.apply()

该方法会改变原数组

var arr1 = [1,2];
var arr2 = ["aa","as"];
arr1.push.apply(arr1, arr2);  

console.log(arr1);    //[1, 2, "aa", "as"]
console.log(arr2);    //["aa", "as"]

n个数组合并成一个数组

var obj = {
    1: [1,2,4,3],
    2: [2,3,4,5],
    3: [4,5,6]
}
Object.values(obj).reduce((a,b) => [...a, ...b], [])

图片描述

数组排列组合

arr = [
        ['a', 'b'],
        ['1', '2'],
        ['x', 'y'],
    ]
    results = [];
    result = [];
    doExchange(arr, 0);
    function doExchange(arr, index){
        for (var i = 0; i<arr[index].length; i++) {
            result[index] = arr[index][i];
            if (index != arr.length - 1) {
                doExchange(arr, index + 1)
            } else {
                results.push(result.join(','))
            }
        }
    }

    console.log( results);

图片描述


不忘初心
645 声望619 粉丝

愚痴的人,一直想要别人了解他。有智慧的人,却努力的了解自己。