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 puts all the elements in an array into the same object. for example

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

The length of two arrays is always the same, but the length of the arrays can increase or decrease. How in...

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

None of the posts below would help answer my question:

Javascript recursion to create a JSON object

Scroll recursively to create a property list

+35
javascript
source share
9 answers

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); 

Alternatively, you can use Object.assign

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

or object distribution 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 use lodash, there is _.zipObject for this particular type of thing.

+57
source share

Using ECMAScript2015:

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

(EDIT) Previously misunderstood OP to get an array:

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

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 the right type. var obj = {}; //loop through the arrays using the first one 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); 
+2
source share

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]} ) ) ); 
+2
source share

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

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

Run fragment:

 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 information.

+1
source share

Use a loop:

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

To achieve the expected result, use below

 var newParamArr = ["Name", "Age", "Email"]; var paramVal = ["Jon", "15", " jon@gmail.com "]; var obj = [] for (var i = 0; i < newParamArr.length; i++) { obj = obj + newParamArr[i] + ":" + paramVal[i] + ","; } obj = "{" + obj.substring(0, obj.length - 1) + "}" console.log(obj) 

https://codepen.io/nagasai/pen/pbXyKR

0
source share

I needed this in several 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} 
0
source share

You can do this using the new JavaScript 6 feature:

For example:

 var keys = ['zahid', 'fahim', 'hasib' ,'iftakhar']; var ages = [11, 22, 33 ,31]; var testObject = { [keys.shift()] : ages.shift(), [keys.shift()] : ages.shift(), [keys.shift()] : ages.shift(), [keys.shift()] : ages.shift() }; console.log(testObject); // { zahid: 11, fahim: 22, hasib: 33, iftakhar: 31 } 
-one
source share

All Articles