从键数组和值数组创建对象

我有两个数组: newParamArrparamVal

newParamArr数组中的示例值: [ "Name", "Age", "Email" ]

paramVal数组中的示例值: [ "Jon", 15, "jon@gmail.com" ]

我需要创建一个 JavaScript 对象,将数组中的所有项放在同一个对象中。例如 { [newParamArr[0]]: paramVal[0], [newParamArr[1]]: paramVal[1], ... }

在这种情况下,结果应该是 { Name: "Jon", "Age": 15, "Email": "jon@gmail.com" }

两个数组的长度总是相同的,但是数组的长度可以增加也可以减少。这意味着 newParamArr.length === paramVal.length将始终保持不变。

以下的帖子都无法回答我的问题:

用于创建 JSON 对象的 Javascript 递归

循环遍历对象以生成属性列表

85328 次浏览

Using ECMAScript2015:

const obj = newParamArr.reduce((obj, value, index) => {
obj[value] = paramArr[index];
return obj;
}, {});

(EDIT) Previously misunderstood the OP to want an array:

const arr = newParamArr.map((value, index) => ({[value]: paramArr[index]}))

Use a loop:

var result = {};
for (var i = 0; i < newParamArr.length; i++) {
result[newParamArr[i]] = paramArr[i];
}

var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]


var result = {};
keys.forEach((key, i) => result[key] = values[i]);
console.log(result);

Alternatively, you can use Object.assign

result = Object.assign(...keys.map((k, i) => ({[k]: values[i]})))

or the object spread syntax (ES2018):

result = keys.reduce((o, k, i) => ({...o, [k]: values[i]}), {})

or Object.fromEntries (ES2019):

Object.fromEntries(keys.map((_, i) => [keys[i], values[i]]))

In case you're using lodash, there's _.zipObject exactly for this type of thing.

The following worked for me.

//test arrays
var newParamArr = [1, 2, 3, 4, 5];
var paramVal = ["one", "two", "three", "four", "five"];


//create an empty object to ensure it's the right type.
var obj = {};


//loop through the arrays using the first one's length since they're the same length
for(var i = 0; i < newParamArr.length; i++)
{
//set the keys and values
//avoid dot notation for the key in this case
//use square brackets to set the key to the value of the array element
obj[newParamArr[i]] = paramVal[i];
}


console.log(obj);

I know that the question is already a year old, but here is a one-line solution:

Object.assign( ...newParamArr.map( (v, i) => ( {[v]: paramVal[i]} ) ) );

You can use Object.assign.apply() to merge an array of {key:value} pairs into the object you want to create:

Object.assign.apply({}, keys.map( (v, i) => ( {[v]: values[i]} ) ) )

A runnable snippet:

var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]


var result =  Object.assign.apply({}, keys.map( (v, i) => ( {[v]: values[i]} ) ) );
console.log(result); //returns {"foo": 11, "bar": 22, "baz": 33}

See the documentation for more

I needed this in a few places so I made this function...

function zip(arr1,arr2,out={}){
arr1.map( (val,idx)=>{ out[val] = arr2[idx]; } );
return out;
}




console.log( zip( ["a","b","c"], [1,2,3] ) );


> {'a': 1, 'b': 2, 'c': 3}

This one works for me.

    var keys = ['foo', 'bar', 'baz'];
var values = [11, 22, 33]
var result = {};
keys.forEach(function(key, i){result[key] = values[i]});
console.log(result);

Object.fromEntries takes an array of key, value tuples and return the zipped result as object, you can then use it as follow:

    const keys = ["a","b","c"];
const values = [1,2,3];
Object.fromEntries(keys.map((key, index)=> [key, values[index]])); // {a: 1, b: 2, c: 3}