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:
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.
javascript
Output:
Python3
Output:
Python3
php
data.get('key')
Parameters: This function accept a single parameter as mentioned above and described below:
// It is used to import collect.js library
const collect = require('collect.js');
const collection = collect({
firstname: 'Jhon',
lastname: 'Carter',
});
console.log(collection.get('firstname'));
JhonExample 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'));
nullExample 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:
cReference: https://collect.js.org/api/get.html