 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Building a Map from 2 arrays of values and keys in JavaScript
Suppose, we have two arrays −
const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"];
We are required to write a JavaScript function that takes in the keys and the values array and maps the values to the corresponding keys. The output should be −
const map = {    0 => 'first',    4 => 'second',    2 => 'third',    3 => 'fourth',    1 => 'fifth' }; Example
Following is the code −
const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"]; const buildMap = (keys, values) => {    const map = new Map();    for(let i = 0; i < keys.length; i++){       map.set(keys[i], values[i]);    };    return map; }; console.log(buildMap(keys, values));  Output
This will produce the following output in console −
Map(5) {    0 => 'first',    4 => 'second',    2 => 'third',    3 => 'fourth',    1 => 'fifth' }Advertisements
 