Last Updated : 03 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:
Example 1: In this example, we are getting an object by passing two arrays in the _.zipObject() method.
JavaScript
const _ = require('lodash');
let x = ['a', 'b', 'c'];
let y = [1, 2, 3]
let obj = _.zipObject(x, y)
console.log(obj);
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.
JavaScript
const _ = require('lodash');
let x = ['name', ';language', 'used'];
let y = ['lodash', 'JavaScript', 'nodejs']
let obj = _.zipObject(x, y)
console.log(obj);
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.
JavaScript
const _ = require('lodash');
let x = ['a', 'b', 'c', 'd'];
let y = [1, 2, 3]
let obj = _.zipObject(x, y)
console.log(obj);
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.
JavaScript
const _ = require('lodash');
let x = ['a', 'b', 'c'];
let y = [1, 2, 3, 4]
let obj = _.zipObject(x, y)
console.log(obj);
Output:
{ a: 1, b: 2, c: 3 }
Note: This will not work in normal JavaScript because it requires the library lodash to be installed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4