We have given two arrays containing keys and values and the task is to store it as a single entity in the form key => value in JavaScript. In JavaScript, the array is a single variable that is used to store different elements. It is usually used once we need to store a list of parts and access them by one variable. We can store key => value array in JavaScript Object using methods discussed below:
Method 1: In this method we will use Object to store key => value in JavaScript. Objects, in JavaScript, is the most important data-type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScript’s primitive data-types(Number, String, Boolean, null, undefined and symbol). Objects are more complex and each object may contain any combination of these primitive data-types as well as reference data-types.
Approach: We will traverse through the whole array and one by one add the keys from the keys (array) and the corresponding values from values (array) in the Object.
Syntax:
for(var i = 0; i < keys.length; i++){
// obj = Object
// keys = key array
// values = value array
obj[keys[i]] = values[i];
}
Example:
<script> // An array of keys var keys = [1, 2, 3]; // An array of values var values = ["GeeksforGeeks", "Computer", "Science"]; // Object created var obj = {}; // Using loop to insert key // value in Object for(var i = 0; i < keys.length; i++){ obj[keys[i]] = values[i]; } // Printing object for (var key of Object.keys(obj)) { document.write(key + " => " + obj[key] + "</br>") } </script> |
Output:
1 => GeeksforGeeks 2 => Computer 3 => Science
Method 2: In this method we will use Map to store key => value in JavaScript. The map is a collection of elements where each element is stored as a key, value pair. Map objects can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key, value pair in the same order as inserted.
Approach: We will traverse through the whole array and one by one add the keys from the keys (array) and the corresponding values from values (array) in the map.
Syntax:
for(var i = 0; i < keys.length; i++){
// mp = Map
// keys = key array
// values = value array
map.set(keys[i], values[i];
}
Example:
<script> // An array of keys var keys = [5, 2, 3, 6, 10]; // An array of values var values = ["Geeks", "for", "Geeks", "Course", "Algorithm"]; // Map created var map = new Map(); // Using loop to insert key // value in map for(var i = 0; i < keys.length; i++){ map.set(keys[i], values[i]); } // Printing for (var key of map.keys()) { document.write(key + " => " + map.get(key) + "</br>") } </script> |
Output:
5 => Geeks 2 => for 3 => Geeks 6 => Course 10 => Algorithm
Recommended Posts:
- How to store all dates in an array present in between given two dates in JavaScript ?
- Atomics.store( ) In JavaScript
- How to store JavaScript functions in a queue and execute in that order?
- Where does PHP store the error log? (php5, apache, fastcgi, cpanel)
- Introduction to Redux (Action, Reducers and Store)
- How to store deployment configuration files in Node.js ?
- How to store password securely in your local/custom database in Node.js ?
- How to Sign In with raw password when password store in the database are hashed form in Node.js ?
- How to Build Progressive Web Application and Submit it to the Play Store?
- Why Laravel Aimeos Is a Good Choice for Building an Online Store ?
- How to convert a pixel value to a number value using JavaScript ?
- RMS Value Of Array in JavaScript
- Sort array of objects by string property value in JavaScript
- Max/Min value of an attribute in an array of objects in JavaScript
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript?
- JavaScript TypeError - Reduce of empty array with no initial value
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to get hex color value of RGB value ?
- How to change an element color based on value of the color picker value using onclick?
- How to push a value based on matched value in PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


