Javascript equivalent of Python's zip function

前端 未结 18 1661
天命终不由人
天命终不由人 2020-11-21 07:40

Is there a javascript equivalent of Python\'s zip function? That is, given multiple arrays of equal lengths create an array of pairs.

For instance, if I have three

18条回答
  •  情话喂你
    2020-11-21 08:05

    Check out the library Underscore.

    Underscore provides over 100 functions that support both your favorite workaday functional helpers: map, filter, invoke — as well as more specialized goodies: function binding, javascript templating, creating quick indexes, deep equality testing, and so on.

    – Say the people who made it

    I recently started using it specifically for the zip() function and it has left a great first impression. I am using jQuery and CoffeeScript, and it just goes perfectly with them. Underscore picks up right where they leave off and so far it hasn't let me down. Oh by the way, it's only 3kb minified.

    Check it out:

    _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
    // returns [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
    

提交回复
热议问题