VOOZH about

URL: https://www.geeksforgeeks.org/javascript/lodash-_-zipobject-method/

⇱ Lodash _.zipObject() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lodash _.zipObject() Method

Last Updated : 3 Sep, 2024

Lodash _.zipObject() method is used to combine two arrays into an object one array as keys and the other as values.

Syntax:

_.zipObject([props=[]], [values=[]]);

Parameters:

  • [props=[]](array): This parameter holds the property identifiers.
  • [values=[]](array): This parameter holds the property values.

Return Value:

  • This method returns an object containing key values corresponding to the given array.

Example 1: In this example, we are getting an object by passing two arrays in the _.zipObject() method.

Output:

{ a: 1, b: 2, c: 3 }

Example 2:  In this example, we are getting an object by passing two arrays in the _.zipObject() method.

Output:

{ name: 'lodash', ';language': 'JavaScript', used: 'nodejs' }

Example 3: In this example, If you pass an extra key and no values for it, it will put undefined value associated with that key. 

Output:

{ a: 1, b: 2, c: 3, d: undefined }

Example 4: In this example, If you pass an extra value and no key for it, it will ignore that value. 

Output:

{ a: 1, b: 2, c: 3 }

Note: This will not work in normal JavaScript because it requires the library lodash to be installed.

Comment