Collect.js | combine() Method
Last Updated :
16 Aug, 2021
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection.
The combine() method combines two different arrays with keys-values.
Installation:
- Collect.js can be installed via NPM:
npm install --save collect.js
- You can also use CDN of collect.js
<script src="https://cdnjs.com/libraries/collect.js"></script>
Syntax:
collect(keys).combine(values)
Parameters: The collect() takes one argument that is keys converted into the collection and then combine() function takes one argument that are values
Return Value: Returns an object.
Below example illustrate the combine() methodin JavaScript:
Example: Here collect = require(‘collect.js’) is used to import the collect.js library into the file.
javascript
const collect = require( 'collect.js' );
let keys = [ 'name' , 'language' ]
let values = [ 'collect.js' , 'js' ]
const collection = collect(keys);
const object = collection.combine(values)
let newObject = object.all();
console.log( "Single array: " , newObject);
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...