How to store a key=> value array in JavaScript ?

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

5 => Geeks
2 => for
3 => Geeks
6 => Course
10 => Algorithm

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.