Giter Site home page Giter Site logo

proposal-array-zip's Introduction

Array.zip for JavaScript

Champions: Marco Ippolito

Author: Marco Ippolito

Stage: 0

This is a proposal to add the static method zip to JavaScript's built-in Array class.

Motivation

Addressing element-wise array combination in JavaScript is a common problem. The existing solutions in JavaScript lack ergonomic and performance. The absence of a dedicated Array.zip method leaves developers resorting to intricate workarounds. These workarounds are not efficient and potentially potentially error prone.

Comparison with other languages

See other languages document to get overview of Array.zip methods in other languages.

Proposal

The Array.zip method enriches the JavaScript Array class with a tool for element-wise array combination.

This would add the following method:

  • Array.zip(...arrays)
  • Array.zip(...arrays, mapFn)
  • Array.zip(...arrays, mapFn, thisArg)

Parameters

  • args - Arrays to zip.
  • mapFn - Function to call on every element of the zipped arrays. It accepts the following arguments:
    • ...args - N-th elements of the zipped arrays in order.
    • index - The index of the element in the original array.
    • array - The original array.
  • thisArg - Value to use as this when executing mapFn.

Examples

// zips two arrays
Array.zip(['a', 'b', 'c'], [1, 2, 3]) // [['a', 1], ['b', 2], ['c', 3]]

// zips three arrays
Array.zip(['a', 'b', 'c'], [1, 2, 3], [true, false, true]) // [['a', 1, true], ['b', 2, false], ['c', 3, true]]

If arrays have different sized, the resulting array will have the length of the shortest array:

Array.zip(['a', 'b', 'c'], [1, 2]) // [['a', 1], ['b', 2]]

Using the mapFn argument:

Array.zip(['a', 'b', 'c'], [1, 2, 3], (a, b, index, array) => ({ [a]: b })) // [{ a: 1}, {b: 2}, {c: 3}]

Using the mapFn with 3 arguments:

Array.zip(['a', 'b', 'c'], [1, 2, 3], [true, false, true], (a, b, c, index, array) => ({ [a]: c ? b : null })) // [{ a: 1}, {b: null}, {c: 3}]

Using the mapFn and thisArg arguments:

Array.zip(['a', 'b', 'c'], [1, 2, 3], (a, b, index, array)  =>  ({ [a]: b + this.offset }), { offset: 10 }) // [{ a: 11}, {b: 12}, {c: 13}]

proposal-array-zip's People

Contributors

marco-ippolito avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.