Thursday, January 7, 2016

Underscore.js(_) : Difference between _.where() and _.findwhere()


Underscore.js is a utility-belt JavaScript library for JavaScript that provides a lot of the functional programming. You can easily manipulate data collection to sort and display it.

_.where ():  

where() function always return the object or list of object contains the specific property from collection of object.

Syntax:
_.where(list, filters);

·         list – collection of item
·         filters – parameters which contains conditions

Here is a collection of objects:

var goal = [
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 2,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "1"
    },
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "2"
    },
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 2,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "3"
    },
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "4"
    }
]


var filteredGoal = _.where(goal, { achievability: "3" });

filteredGoal is having all matching objects, which have achievability property value 3.

[
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "2"
    },
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "4"
    }
]


_.findwhere() : findwhere () function always returns the first matched object in collection.

Syntax:

_. findwhere (list, filters);

·         list – collection of item
·         filters – parameters which contains conditions

var filteredGoal = _.findwhere(goal, { achievability: "3" });

filteredGoal is having only first matching record of goal collections

    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "2"
    }

Additional link: 

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...