CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-zipobject

The lodash zipObject array utility function for creating objects from arrays of keys and values

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

lodash.zipobject

The 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.

Package Information

  • Package Name: lodash.zipobject
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.zipobject

Core Imports

var zipObject = require('lodash.zipobject');

For ES modules (if using a bundler that supports it):

import zipObject from 'lodash.zipobject';

Basic Usage

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 }

Capabilities

Object Creation from Arrays

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/undefined
  • values (Array, optional): Array of property values. Defaults to empty array if not provided or null/undefined

Returns:

  • Object: A new object with properties created from combining the props and values arrays

Behavior:

  • When arrays have different lengths, properties without corresponding values are assigned undefined
  • Null or undefined input arrays are treated as empty arrays
  • The function creates a new object and does not modify the input arrays
  • Uses SameValueZero equality comparison internally for property assignment
  • Supports any valid JavaScript property names (strings) and any JavaScript values

Usage 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 }

docs

index.md

tile.json