Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptCollect.js | zip() Function

Collect.js | zip() Function

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments