The zip() function is used to combine the values of given array with the values of original collection at a given index. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.
Syntax:
data.zip(value)
Parameters: This function accept a single parameter as mentioned above and described below:
- values : This parameter holds the values in the new array.
Return value : Return a new collection by margin the value with the main collection one by one.
Below examples illustrate the zip() function in collect.js:
Example 1: Here in this example, we take a collection and then using the zip() function we matched them with a array of corresponding items of a new collection using the zip() method
Javascript
// It is used to import collect.js library const collect = require( 'collect.js' ); const fruits = collect([ 'Apple' , 'Banana' ]); const quantity = fruits.zip([10, 15]); console.log(quantity.all()); |
Output:
[ Collection { items: [ 'Apple', 10 ] }, Collection { items: [ 'Banana', 15 ] } ]
Example 2:
Javascript
// It is used to import collect.js library const collect = require( 'collect.js' ); const people = collect([ 'Ram' , 'Sham' ]); const age = people.zip([20, 25]); console.log(age.all()); |
Output:
[ Collection { items: [ 'Ram', 20 ] }, Collection { items: [ 'Sham', 25 ] } ]
Reference: https://collect.js.org/api/zip.html