数组处理

35 阅读1分钟
分割成多维数组
function group(array, subGroupLength) {
    var index = 0;
    var newArray = [];

    while(index < array.length) {
        newArray.push(array.slice(index, index += subGroupLength));
    }

    return newArray;
}

var countries = [];
var groupedCountries = group(countries, 3);
数组打平
/** 打平数组 */
treeToArray(tree) {
    let newArr = []
    const expanded = datas => {
        if (datas?.length) {
            datas.forEach(it => {
                const { children, ...others } = it
                newArr.push(others)
                expanded(children)
            })
        }
    }
    expanded(tree)
    return newArr
}