Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
communitycreator

How to use the fromPairs() method in Lodash

Shubham Singh Kshatriya

Overview

The _.fromPairs() method in Lodash creates an object composed of an array of key-value pairs.

Syntax

_.fromPairs(array)
Syntax of _.fromPairs() method

Parameters

This method accepts the following parameters:

  • array: This is the array containing the key-value pairs.

Return value

This method returns an object.

Example

Let’s look at an example of the _.fromPairs() method in the code snippet below:

Console
Implementation of the _.fromPairs() method

Explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Line 2: We create an array and initialize it.
  • Line 5: We use the _.fromPairs() method to create an object.
  • Line 8: We print the output to the console.

Output

In the output, we see an object composed of the key-value pairs of the array.

    RELATED TAGS

    lodash
    communitycreator

    CONTRIBUTOR

    Shubham Singh Kshatriya
    RELATED COURSES

    View all Courses

    Keep Exploring