The lodash zipObject array utility function for creating objects from arrays of keys and values
npx @tessl/cli install tessl/npm-lodash-zipobject@4.1.0The lodash zipObject array utility function as a standalone Node.js module. This package provides the zipObject function that creates objects by combining two arrays - one containing property names and another containing corresponding values. This method is like _.fromPairs except that it accepts two arrays instead of a single array of pairs.
npm install lodash.zipobjectvar zipObject = require('lodash.zipobject');For ES modules (if using a bundler that supports it):
import zipObject from 'lodash.zipobject';var zipObject = require('lodash.zipobject');
// Create an object from arrays of keys and values
var keys = ['a', 'b', 'c'];
var values = [1, 2, 3];
var result = zipObject(keys, values);
// => { 'a': 1, 'b': 2, 'c': 3 }
// Handle mismatched array lengths
var result2 = zipObject(['x', 'y', 'z'], [10, 20]);
// => { 'x': 10, 'y': 20, 'z': undefined }
// Handle empty or null inputs
var result3 = zipObject(null, [1, 2, 3]);
// => {}
var result4 = zipObject(['a', 'b'], null);
// => { 'a': undefined, 'b': undefined }Creates an object by combining two arrays - one of property identifiers and one of corresponding values. Available since lodash version 0.4.0.
/**
* Creates an object composed from arrays of property names and values.
* @static
* @memberOf _
* @since 0.4.0
* @category Array
* @param {Array} [props=[]] The property identifiers.
* @param {Array} [values=[]] The property values.
* @returns {Object} Returns the new object.
*/
function zipObject(props, values);Parameters:
props (Array, optional): Array of property identifiers/keys. Defaults to empty array if not provided or null/undefinedvalues (Array, optional): Array of property values. Defaults to empty array if not provided or null/undefinedReturns:
Object: A new object with properties created from combining the props and values arraysBehavior:
undefinedUsage Examples:
var zipObject = require('lodash.zipobject');
// Basic usage
zipObject(['a', 'b'], [1, 2]);
// => { 'a': 1, 'b': 2 }
// Mismatched lengths - extra keys get undefined
zipObject(['fred', 'barney', 'pebbles'], [30, 40]);
// => { 'fred': 30, 'barney': 40, 'pebbles': undefined }
// Mismatched lengths - extra values are ignored
zipObject(['a', 'b'], [1, 2, 3, 4]);
// => { 'a': 1, 'b': 2 }
// Empty arrays
zipObject([], []);
// => {}
// Various value types
zipObject(['str', 'num', 'bool', 'obj', 'arr'],
['hello', 42, true, {x: 1}, [1, 2, 3]]);
// => { 'str': 'hello', 'num': 42, 'bool': true, 'obj': {x: 1}, 'arr': [1, 2, 3] }
// Null/undefined handling
zipObject(null, [1, 2, 3]);
// => {}
zipObject(['a', 'b', 'c'], undefined);
// => { 'a': undefined, 'b': undefined, 'c': undefined }