我需要将数组或数组数组转换为具有从名称数组命名的键的对象.例:
//given names
names = ['first', 'second', 'third', 'fourth']
//array
param = [1, 2, 3, 4]
//becomes
result = {first: 1, second: 2, third: 3, fourth: 4}
//array of arrays
param = [
[1, 2, 3, 4],
[-4, 3, 1, 32],
]
//becomes
result = [
{first: 1, second: 2, third: 3, fourth: 4},
{first: -4, second: 3, third: 1, fourth: 32},
]
Run Code Online (Sandbox Code Playgroud)
我目前的解决方案是:
var names = ['first', 'second', 'third', 'forth'];
function arrayToObject(array, names) {
var result = {};
for …Run Code Online (Sandbox Code Playgroud)