The object
function from the Underscore library in JavaScript is used to convert arrays into objects. Here is how to use it:
First, make sure that you have included the Underscore library in your project. You can do this by including the following line in your HTML file:
38 chars2 lines
Once you have included the library, you can use the object
function by passing an array as an argument:
index.tsx171 chars11 lines
In the above example, the myArray
variable is an array of arrays, where each child array represents a key-value pair. The _.object
function takes this array and converts it into an object, where the first item in each child array is the key and the second item is the value.
Note that if there are duplicate keys in the array, the last value wins.
gistlibby LogSnag