CtrlAltDelete CtrlAltDelete - 3 months ago 10
Javascript Question

Creating a JavaScript Object from two arrays

I have two arrays:

newParamArr[i]
and
paramVal[i]
.

Example values in the
newParamArr[i]
array:
["Name", "Age", "Email"]


Example values in the
paramVal[i]
array:
["Jon", "15", "jon@gmail.com"]


I need to create a JavaScript object that places all of the items in the array in the same object. For example

{newParamArr[0]:paramVal[0], newParamArr[1]:paramVal[1], ...}


The lengths of the two arrays are always the same, but the length of arrays can increase or decrease. As in...

newParamArr.length === paramVal.length
will always returns true.

None of the below posts could help to answer my question:

Javascript Recursion for creating a JSON object

Recursively looping through an object to build a property list

Answer

Do you mean this?

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

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

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

Comments