Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

underscore.js: _.zip.apply example

I would like to see an example of _.zip.apply using underscore.js.

In the underscore documentation is written:

If you're working with a matrix of nested arrays, zip.apply can transpose the matrix in a similar fashion.

However, the documentation provides no example.

like image 435
antonjs Avatar asked May 01 '12 07:05

antonjs


People also ask

How do you use underscore in JavaScript?

Adding Underscore to a Node. js modules using the CommonJS syntax: var _ = require('underscore'); Now we can use the object underscore (_) to operate on objects, arrays and functions.

How does _ each work JavaScript?

each. The _each method does exactly what it sounds like. It works on collections (arrays or objects), and will iterate over each element in the collection invoking the function you specified with 3 arguments (value, index, list) with index being replaced by key if used on an object.

What does _ Reduce do?

The _. reduce() function is an inbuilt function in Underscore. js that is used to transform an array's/object's properties into one single value or is used to create a single result from a given list of values.

What is _ template?

The _. template() function is an inbuilt function in the Underscore. js library of JavaScript which is used to compile JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources.


1 Answers

It's your standard use of apply:

_.zip.apply(null, [ ['foo','bar'], [0,1] ])

This would result in the following:

[['foo', 0], ['bar', 1]]
like image 55
Sampson Avatar answered Nov 02 '22 22:11

Sampson