Menu

gitpiper

zipObject javascript Code Snippet in 2024

arrayobjectintermediate

Last Updated: 15 April 2024

Associates properties to values, given array of valid property identifiers and an array of values.

  • Use Array.prototype.reduce() to build an object from the two arrays.
  • If the length of props is longer than values, remaining keys will be undefined.
  • If the length of values is longer than props, remaining values will be ignored.
const zipObject = (props, values) => props.reduce((obj, prop, index) => ((obj[prop] = values[index]), obj), {});
zipObject(['a', 'b', 'c'], [1, 2]); // {a: 1, b: 2, c: undefined} zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}

javascript snippet similar to zipObject For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️