Underscore.js _.object() Function Last Updated : 24 Nov, 2021 Comments Improve Suggest changes Like Article Like Report _.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 listThe values Return value: It returns an array having list of pairs from the keys and values passed. Examples: 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. html <!-- Write HTML code here --> <html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script type="text/javascript"> console.log(_.object(['Akash', 'Amit', 'Aviral'], [01, 02, 03])); </script> </body> </html> Output: 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. html <!-- Write HTML code here --> <html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script type="text/javascript"> console.log(_.object([['Akash', 'Amit'], [01, 02], ['pass', 'pass']])); </script> </body> </html> Output: Using _.object() function in real lifeIn 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. html <!-- Write HTML code here --> <html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script type="text/javascript"> console.log(_.object([['Name', 'Amit'], ['rollNo', 02], ['pass', 'yes']])); </script> </body> </html> Output: 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. html <!-- Write HTML code here --> <html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script type="text/javascript"> console.log(_.object(['Name', 'rollNo', 'pass'], ['Amit', 02, 'yes'])); </script> </body> </html> 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: html <!-- Write HTML code here --> <script type="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> An example is shown below: Create Quiz Comment S Sakshi98 Follow 0 Improve S Sakshi98 Follow 0 Improve Article Tags : JavaScript Web Technologies JavaScript - Underscore.js Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like