js多个数组(3个以上)合并成json形式

动态传入数组,动态计算 key,即可,代码如下:

const arr1 = [1, 2, 3, 4, 5];
const arr2 = ['a1', 'a2', 'a3', 'a4', 'a5'];
const arr3 = ['b1', 'b2', 'b3', 'b4', 'b5'];
const arr4 = ['c1', 'c2', 'c3', 'c4', 'c5'];
const arr5 = [11, 22, 33, 44, 55];

function parse (...arrList) {
    const result = []
    for (let i = 0, l = arrList.length, arr, key; i < l; i++) {
        arr = arrList[i]
        key = String.fromCharCode(97 + i)
        for (let j = 0, l = arr.length; j < l; j++) {
            if (!result[j]) {
                result[j] = {}
            }
            result[j][key] = arr[j]
        }
    }
    return result
}

parse(arr1, arr2, arr3, arr4, arr5)

你可能感兴趣的:(js多个数组(3个以上)合并成json形式)