Underscore.js | _.object()

_.object() function:

  • It converts the array elements into objects.
  • We can either pass all keys and then all the values or we can pass the key along with their value in pairs.
  • It is used if we have more than one array but we want to make/ form a relation between these arrays.
  • Also, size of the arrays is not necessary to be known before hand.

Syntax:

_.object(list, [values])

Parameters:
It takes two arguments:



  • The list
  • The values

Return value:
It returns an array having list of pairs from the keys and values passed.

Examples:

  1. Passing 2 lists to _.object() function:
    The ._object() function takes the element from the list one by one and makes it as an object with the value given in the other list. It takes the first elements of both the arrays and make them a single object by taking first array’s elements as the key and the other array’s as value. It will work only on first 2 arrays passed even if we pass a third array.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.object(['Akash', 'Amit', 'Aviral'], [01, 02, 03]));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  2. Passing key along with it’s value in a single list to _.object() function:
    The ._object() function takes the element from the list one by one and makes it as an object with the value given along with it in the same list. It takes all the elements in the first array and then form it a single object. It treats first array passed as a single object. We can pass n number of lists in this function.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.object([['Akash', 'Amit'], [01, 02], ['pass', 'pass']]));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  3. Using _.object() function in real life
    In this we use the second type of syntax mentioned above. If we want to obtain a database of a student named Amit (here) so we can just pass the parameters like his name, roll number and his result along with his details like his roll number is 2 and he is passed to the next class.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.object([['Name', 'Amit'], ['rollNo', 02], ['pass', 'yes']]));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  4. Another way to obtain the same result as in example third using _.object() function:
    In this also the result obtains will be same but the method of passing the key and value is different like here we pass all the keys in one array. Then, in the other array we pass all the values. Then the first element of the first array forms the first key and the first element of the second element of the second array as the value.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.object(['Name', 'rollNo', 'pass'], ['Amit', 02, 'yes']));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

NOTE:
These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added.
So, add the given links to your HTML file and then run them.
The links is as follow:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- Write HTML code here -->
<script type="text/javascript" 
</script>

chevron_right


An example is shown below:



My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.