I have two arrays: newParamArr and paramVal.
Example values in the newParamArr array: [ "Name", "Age", "Email" ].
Example values in the paramVal array: [ "Jon", 15, "[email protected]" ].
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], ... }.
In this case, the result should be { Name: "Jon", "Age": 15, "Email": "[email protected]" }.
The lengths of the two arrays are always the same, but the length of arrays can increase or decrease. That means newParamArr.length === paramVal.length will always hold.
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
10 Answers 10
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.
5 Comments
forEach seems to be quite speedy as well: jsperf.app/sezabi.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]}))
2 Comments
reduce instead.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}
Comments
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]} ) ) );
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);
1 Comment
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
Comments
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);
Comments
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}
Comments
Use a loop:
var result = {};
for (var i = 0; i < newParamArr.length; i++) {
result[newParamArr[i]] = paramArr[i];
}
Comments
A function using Array.reduce
const keys = ['foo', 'bar', 'baz'];
const values = [11, 22, 33];
console.log(combine2Object({keys, values}));
console.log(combine2Object());
function combine2Object( {keys = [`empty`], values = [true]} = {} ) {
return keys.reduce( (acc, v, i) => ({...acc, [v]: values[i]}), {});
}
But, but, but, my teacher says I can't use loops!
Ok.
const keys = ['foo', 'bar', 'baz'];
const values = [11, 22, 33];
const obj = {};
obj[keys[0]] = values[0];
obj[keys[1]] = values[1];
obj[keys[2]] = values[2];
console.log(obj);
// or if you want to impress your teacher
// even more
const wowzer = {
[keys[0]]: values[0],
[keys[1]]: values[1],
[keys[2]]: values[2],
};
console.log(`Wowzer! `, wowzer);
Now ask your teacher what to do when you can't use loops and the length of keys-/values arrays is not known up front.
newParamArrandparamValand the wanted result.newParamArr[i]an array with the content["Name", "Age", "Email"]or is the arraynewParamArr = ["Name", "Age", "Email"]?