Collect.js | get() Function
Last Updated :
05 Jun, 2020
The get() function is used to return a value that is present at a particular key. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.
Syntax:
data.get('key')
Parameters: This function accept a single parameter as mentioned above and described below:
Key: This parameter holds the key name that define the value of that key.
Return value: Return the value of the mentioned key.
Below examples illustrate the get() function in collect.js:
Example 1: Here in this example, we take a collection and then using the get() function, it returns the value using the key.
const collect = require( 'collect.js' );
const collection = collect({
firstname: 'Jhon' ,
lastname: 'Carter' ,
});
console.log(collection.get( 'firstname' ));
|
Output:
Jhon
Example 2: If the key value is not present, then it will return null.
/ / It is used to import collect.js library
const collect = require( 'collect.js' );
const collection = collect({
firstname: 'Jhon' ,
lastname: 'Carter' ,
});
console.log(collection.get( 'middlename' ));
|
Output:
null
Example 3:
/ / It is used to import collect.js library
const collect = require( 'collect.js' );
const collection = collect([ 'a' , 'b' , 'c' ]);
console.log(collection.get( 2 ));
|
Output:
c
Reference: https://collect.js.org/api/get.html
Share your thoughts in the comments
Please Login to comment...