Open In App

Collect.js | get() Function

Last Updated : 05 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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. javascript
    // 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'));
    
    Output:
    Jhon
    Example 2: If the key value is not present, then it will return null. Python3
    // 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: Python3
    // It is used to import collect.js library
    const collect = require('collect.js');
    
    const collection = collect(['a', 'b', 'c']);
    
    console.log(collection.get(2));
    
    php
    
    
    Output:
    c
    Reference: https://collect.js.org/api/get.html

    Next Article

    Similar Reads