
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to combine 2 arrays into 1 object in JavaScript
Let’s say, we have two arrays of equal lengths and are required to write a function that maps the two arrays into an object. The corresponding elements of the first array becomes the corresponding keys of the object and the elements of the second array become the value.
We will reduce the first array, at the same time accessing elements of the second array by index. The code for this will be −
Example
const keys = [ 'firstName', 'lastName', 'isEmployed', 'occupation', 'address', 'salary', 'expenditure' ]; const values = [ 'Hitesh', 'Kumar', false, 'Frontend Developer', 'Tilak Nagar, New Delhi', 90000, 45000 ]; const combineArrays = (first, second) => { return first.reduce((acc, val, ind) => { acc[val] = second[ind]; return acc; }, {}); }; console.log(combineArrays(keys, values));
Output
The output in the console will be −
{ firstName: 'Hitesh', lastName: 'Kumar', isEmployed: false, occupation: 'Frontend Developer', address: 'Tilak Nagar, New Delhi', salary: 90000, expenditure: 45000 }
- Related Articles
- How to combine two arrays into an array of objects in JavaScript?
- Combine two different arrays in JavaScript
- How to dynamically combine all provided arrays using JavaScript?
- Converting two arrays into a JSON object in JavaScript
- Converting array of arrays into an object in JavaScript
- JavaScript: Combine highest key values of multiple arrays into a single array
- JavaScript Converting array of objects into object of arrays
- Can we convert two arrays into one JavaScript object?
- How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?
- Combine two arrays in C#
- How to add two arrays into a new array in JavaScript?
- How to convert square bracket object keys into nested object in JavaScript?
- How to deserialize a JSON into Javascript object?
- Stack 1-D arrays as columns into a 2-D array in Numpy
- How to import an Object with Sub Objects and Arrays in JavaScript?

Advertisements