
- 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 store two arrays as a keyvalue pair in one object in JavaScript?
Suppose, we have two arrays of literals of same length like these −
const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false];
We are required to write a JavaScript function that takes in two such arrays.
The function should construct an object mapping the elements of the second array to the corresponding elements of the first array.
We will use the Array.prototype.reduce() method to iterate over the arrays, building the object.
Example
The code for this will be −
const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false]; const mapArrays = (arr1 = [], arr2 = []) => { const res = arr1.reduce((acc,elem,index) =>{ acc[elem]=arr2[index]; return acc; },{}); return res; }; console.log(mapArrays(arr1, arr2));
Output
And the output in the console will be −
{ firstName: 'Rahul', lastName: 'Sharma', age: 23, address: 'Tilak Nagar', isEmployed: false }
- Related Articles
- How to subtract elements of two arrays and store the result as a positive array in JavaScript?
- Can we convert two arrays into one JavaScript object?
- How to merge two arrays with objects in one in JavaScript?
- Converting two arrays into a JSON object in JavaScript
- How to merge two object arrays of different size by key in JavaScript
- How to merge two arrays in JavaScript?
- How to join two arrays in JavaScript?
- How to multiply two Arrays in JavaScript?
- How to compare two arrays in JavaScript and make a new one of true and false? JavaScript
- How to pass arrays as function arguments in JavaScript?
- How to combine 2 arrays into 1 object in JavaScript
- How to add two arrays into a new array in JavaScript?
- Deviations in two JavaScript arrays in JavaScript
- How to store the contents of arrays in a file using Java?
- Joining two Arrays in Javascript

Advertisements