create account

Linqjs Tutorial #02 - Lambda Expression (Distinct, OrderBy, SelectCustomizeColumn, ToJSONString) by touhidalam69

View this thread on: hive.blogpeakd.comecency.com
· @touhidalam69 · (edited)
$0.50
Linqjs Tutorial #02 - Lambda Expression (Distinct, OrderBy, SelectCustomizeColumn, ToJSONString)
![linqjs.png](https://cdn.steemitimages.com/DQmecCSuSwCqMYsfGHVn8Fu8LuRsrPu1EtuDMLqApMnHJa2/linqjs.png)

### Repository
[https://github.com/mihaifm/linq](https://github.com/mihaifm/linq)

### What Will I Learn?
- You will learn how to distinct values from array using linqjs.
- You will learn how to order by array values in ascending order.
- You will learn how to order by array values in descending order.
- You will learn how to order by multiple column.
- You will learn how to make a customize column in every array object.
- You will learn how to convert array values to Json string.

### Requirements
- Node.JS
- Basic knowledge of Javascript
- Any suitable code editor

### Difficulty
- Basic/Intermediate

### Tutorial Contents

#### Declare an array which will be used on our following steps of tutorial
> var countryList = [
    { "country": "Afghanistan", "continent": "Asia" },
    { "country": "Bangladesh", "continent": "Asia" },
    { "country": "Belgium", "continent": "Europe" },
    { "country": "Bhutan", "continent": "Asia" },
    { "country": "Bolivia", "continent": "South America" },
    { "country": "Brazil", "continent": "South America" },
    { "country": "Croatia", "continent": "Europe" },
    { "country": "Cuba", "continent": "North America" },
    { "country": "Denmark", "continent": "Europe" },
    { "country": "Egypt", "continent": "Africa" },
    { "country": "Finland", "continent": "Europe" },
    { "country": "France", "continent": "Europe" },
    { "country": "Guatemala", "continent": "North America" },
    { "country": "Guernsey", "continent": "Europe" },
    { "country": "Lebanon", "continent": "Asia" },
    { "country": "Liberia", "continent": "Africa" },
    { "country": "Nepal", "continent": "Asia" },
    { "country": "Netherlands", "continent": "Europe" },
    { "country": "Norway", "continent": "Europe" },
    { "country": "Oman", "continent": "Asia" },
    { "country": "Poland", "continent": "Europe" },
    { "country": "Portugal", "continent": "Europe" },
    { "country": "Puerto Rico", "continent": "North America" },
    { "country": "Qatar", "continent": "Asia" },
    { "country": "Romania", "continent": "Europe" },
    { "country": "Switzerland", "continent": "Europe" },
    { "country": "Taiwan, Province of China", "continent": "Asia" },
    { "country": "Tajikistan", "continent": "Asia" },
    { "country": "Venezuela", "continent": "South America" },
    { "country": "Virgin Islands, U.S.", "continent": "North America" },
    { "country": "Wallis and Futuna", "continent": "Oceania" },
    { "country": "Western Sahara", "continent": "Africa" },
    { "country": "Zimbabwe", "continent": "Africa" }];

We have declared an array of country list. which contains country name and its continent.

#### 1. Distinct
Our target is to find out all continents name from the array countryList and meke a new array on continents.

>var continents = rlinq.from(countryList).distinct('$.continent').select("{continent:$.continent}").toArray();  

Explanation :
- We have passed countryList on the parameter of from function
- We mentioned continent to distinct by passing $.continent(lambda expression) as a parameter of distinct function

Output :
![Result_Distinct.png](https://cdn.steemitimages.com/DQmNyZfJcGCximzewdTpQMVtYgkMNbhGkdpZW8awDAqEihA/Result_Distinct.png)

#### 2. Order by Ascending 
Our target is to organize countryList array in ascending order based on country name.

>var AssendingcountryList = rlinq.from(countryList).orderBy('$.country').toArray();

Explanation :
- We used orderBy function to sort the countryList array in ascending order based on country name.

Output :
![Result_Orderby.png](https://cdn.steemitimages.com/DQmYJ988qjUwSJSS3M47sufkpSWYTwjqtMSBXjafhFFMSWg/Result_Orderby.png)

#### 3. Order by Descending 
Our target is to organize countryList array in Descending order based on country name.

>var DescendingcountryList = rlinq.from(countryList).orderByDescending('$.country').toArray();

Explanation :
- We used orderByDescending function to organize the countryList array in Descending order based on country name.

Output :
![Result_OrderbyDesc.png](https://cdn.steemitimages.com/DQmRz47Nn4krmhUa2Radbc7ozBHKof5a9gYBdYnkEbpot4p/Result_OrderbyDesc.png)

#### 4. Multiple Order by (Thenby)
Our target is to organize countryList array in ascending order based on continent and country name.

>var ThenbycountryList = rlinq.from(countryList).orderBy('$.continent').thenBy('$.country').toArray();

Explanation :
- at first we used orderBy function to organize the countryList array to organize it in Ascending order based on continent. then we used thenBy function to organize it in ascending order based on country name.

ThenbycountryList Values

>0:Object {country: "Egypt", continent: "Africa"}
1:Object {country: "Liberia", continent: "Africa"}
2:Object {country: "Western Sahara", continent: "Africa"}
3:Object {country: "Zimbabwe", continent: "Africa"}
4:Object {country: "Afghanistan", continent: "Asia"}
5:Object {country: "Bangladesh", continent: "Asia"}
6:Object {country: "Bhutan", continent: "Asia"}
7:Object {country: "Lebanon", continent: "Asia"}
8:Object {country: "Nepal", continent: "Asia"}
9:Object {country: "Oman", continent: "Asia"}
10:Object {country: "Qatar", continent: "Asia"}
...

#### 5. Multiple Order by (thenByDescending)
Our target is to organize countryList array in ascending order based on continent and descending order based on country name.

>var ThenByDesccountryList = rlinq.from(countryList).orderBy('$.continent').thenByDescending('$.country').toArray();

Explanation :
- at first we used orderBy function to organize the countryList array to organize it in Ascending order based on continent. then we used thenByDescending function to organize it in descending order based on country name.

ThenByDesccountryList Values

>0:Object {country: "Zimbabwe", continent: "Africa"}
1:Object {country: "Western Sahara", continent: "Africa"}
2:Object {country: "Liberia", continent: "Africa"}
3:Object {country: "Egypt", continent: "Africa"}
4:Object {country: "Tajikistan", continent: "Asia"}
5:Object {country: "Taiwan, Province of China", continent: "Asia"}
6:Object {country: "Qatar", continent: "Asia"}
7:Object {country: "Oman", continent: "Asia"}
8:Object {country: "Nepal", continent: "Asia"}
9:Object {country: "Lebanon", continent: "Asia"}
10:Object {country: "Bhutan", continent: "Asia"}
...

#### 6. Select Customize Column
Our target is to add a customized column which will be added with every single object in the countryList array.

>var countryListwithCustomeColumn = rlinq.from(countryList).select("{country:$.country, country_with_continent:$.continent+'('+$.country+')'}").toArray();

Explanation :
- We mentioned two column country and country_with_continent by passing as parameter of select function. where we defined country columns value and country_with_continent columns Customized value.

countryListwithCustomeColumn Values

>0:Object {country: "Afghanistan", country_with_continent: "Asia(Afghanistan)"}
 1:Object {country: "Bangladesh", country_with_continent: "Asia(Bangladesh)"}
 2:Object {country: "Belgium", country_with_continent: "Europe(Belgium)"}
 3:Object {country: "Bhutan", country_with_continent: "Asia(Bhutan)"}
 4:Object {country: "Bolivia", country_with_continent: "South America(Bolivia)"}
 5:Object {country: "Brazil", country_with_continent: "South America(Brazil)"}
 6:Object {country: "Croatia", country_with_continent: "Europe(Croatia)"}
 7:Object {country: "Cuba", country_with_continent: "North America(Cuba)"}
 8:Object {country: "Denmark", country_with_continent: "Europe(Denmark)"}
 9:Object {country: "Egypt", country_with_continent: "Africa(Egypt)"}
 10:Object {country: "Finland", country_with_continent: "Europe(Finland)"}
...

#### 7. Convert to JsonString
Our target is to make a json of countryList array.

>var countryListJson = rlinq.from(countryList).toJSONString();

Explanation :
- We used toJSONString function, which returns a JSON string of the countryList array.

countryListJson Values

>"[{"country":"Afghanistan","continent":"Asia"},{"country":"Bangladesh","continent":"Asia"},{"country":"Belgium","continent":"Europe"},{"country":"Bhutan","continent":"Asia"},{"country":"Bolivia","continent":"South America"},{"country":"Brazil","continent":"South America"},{"country":"Croatia","continent":"Europe"},{"country":"Cuba","continent":"North America"},{"country":"Denmark","continent":"Europe"},{"country":"Egypt","continent":"Africa"},{"country":"Finland","continent":"Europe"},{"country":"France","continent":"Europe"},{"country":"Guatemala","continent":"North America"},{"country":"Guernsey","continent":"Europe"},{"country":"Lebanon","continent":"Asia"},{"country":"Liberia","continent":"Africa"},{"country":"Nepal","continent":"Asia"},{"country":"Netherlands","continent":"Europe"},{"country":"Norway","continent":"Europe"},{"country":"Oman","continent":"Asia"},{"country":"Poland","continent":"Europe"},{"country":"Portugal","continent":"Europe"},{"country":"Puerto Rico","continent":"North America"},{"country":"Qatar","continent":"Asia"},{"country":"Romania","continent":"Europe"},{"country":"Switzerland","continent":"Europe"},{"country":"Taiwan, Province of China","continent":"Asia"},{"country":"Tajikistan","continent":"Asia"},{"country":"Venezuela","continent":"South America"},{"country":"Virgin Islands, U.S.","continent":"North America"},{"country":"Wallis and Futuna","continent":"Oceania"},{"country":"Western Sahara","continent":"Africa"},{"country":"Zimbabwe","continent":"Africa"}]"

### Curriculum
- [Linqjs Tutorial #01 (Install, Sum, Avg, Min, Max, Select)](https://steemit.com/utopian-io/@touhidalam69/linqjs-tutorial-01-nodejs-with-lambda-expression-install-sum-avg-min-max-select)

### Proof of Work Done
[Tutorial Code](https://github.com/touhidalam69/linqjsTutorial/blob/master/src/02_distinct_orderBy_thenBy_SelectCustomeColumn_toJSONString.js)
GitHub: [https://github.com/touhidalam69/linqjsTutorial](https://github.com/touhidalam69/linqjsTutorial)
👍  , , , , , , , , , , , , , , , , , , , ,
properties (23)
authortouhidalam69
permlinklinqjs-tutorial-02-lambda-expression-distinct-orderby-orderbydescending-thenby-thenbydescending-selectcustomizecolumn
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","javascript","linqjs","lambda"],"image":["https://cdn.steemitimages.com/DQmecCSuSwCqMYsfGHVn8Fu8LuRsrPu1EtuDMLqApMnHJa2/linqjs.png","https://cdn.steemitimages.com/DQmNyZfJcGCximzewdTpQMVtYgkMNbhGkdpZW8awDAqEihA/Result_Distinct.png","https://cdn.steemitimages.com/DQmYJ988qjUwSJSS3M47sufkpSWYTwjqtMSBXjafhFFMSWg/Result_Orderby.png","https://cdn.steemitimages.com/DQmRz47Nn4krmhUa2Radbc7ozBHKof5a9gYBdYnkEbpot4p/Result_OrderbyDesc.png"],"links":["https://github.com/mihaifm/linq","https://steemit.com/utopian-io/@touhidalam69/linqjs-tutorial-01-nodejs-with-lambda-expression-install-sum-avg-min-max-select","https://github.com/touhidalam69/linqjsTutorial/blob/master/src/02_distinct_orderBy_thenBy_SelectCustomeColumn_toJSONString.js","https://github.com/touhidalam69/linqjsTutorial"],"app":"steemit/0.1","format":"markdown","community":"busy"}
created2018-06-22 17:31:33
last_update2018-06-23 12:20:51
depth0
children3
last_payout2018-06-29 17:31:33
cashout_time1969-12-31 23:59:59
total_payout_value0.401 HBD
curator_payout_value0.094 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10,088
author_reputation2,409,777,621,558
root_title"Linqjs Tutorial #02 - Lambda Expression (Distinct, OrderBy, SelectCustomizeColumn, ToJSONString)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,801,125
net_rshares249,611,098,154
author_curate_reward""
vote details (21)
@pckurdu ·
Thank you for the contribution but I think it would be more beneficial if you explain it on a more illustrative example. Such an explanation leads to confusion.
👍  
properties (23)
authorpckurdu
permlinkre-touhidalam69-linqjs-tutorial-02-lambda-expression-distinct-orderby-orderbydescending-thenby-thenbydescending-selectcustomizecolumn-20180623t120052078z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-06-23 12:00:51
last_update2018-06-23 12:00:51
depth1
children1
last_payout2018-06-30 12:00:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length160
author_reputation23,385,816,696,918
root_title"Linqjs Tutorial #02 - Lambda Expression (Distinct, OrderBy, SelectCustomizeColumn, ToJSONString)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,901,188
net_rshares4,145,567,525
author_curate_reward""
vote details (1)
@touhidalam69 ·
$0.02
Thanks @pckurdu, for your comment. Actually i thought developers will be able to understand it in this way.  If you have any confusion on this tutorial, please specify it. I will try to resolve it.
👍  
properties (23)
authortouhidalam69
permlinkre-pckurdu-re-touhidalam69-linqjs-tutorial-02-lambda-expression-distinct-orderby-orderbydescending-thenby-thenbydescending-selectcustomizecolumn-20180623t134024196z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["pckurdu"],"app":"steemit/0.1"}
created2018-06-23 13:40:24
last_update2018-06-23 13:40:24
depth2
children0
last_payout2018-06-30 13:40:24
cashout_time1969-12-31 23:59:59
total_payout_value0.018 HBD
curator_payout_value0.006 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length197
author_reputation2,409,777,621,558
root_title"Linqjs Tutorial #02 - Lambda Expression (Distinct, OrderBy, SelectCustomizeColumn, ToJSONString)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,912,217
net_rshares12,767,396,263
author_curate_reward""
vote details (1)
@yokunjon ·
I thank you for your contribution. Here are my thoughts on your post:

* Tutorials should teach the user something unique, shouldn't show ubiquitous functions and replicate well-documented concepts. Therefore, in the voting phase, your tutorial might not be considered.

* The volume of your tutorial is too sparse. To increase it, please reduce the amount of the filler content (like too long code blocks) and add several (up to 4-5) substantial concepts.

----

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines).

----
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
👍  
properties (23)
authoryokunjon
permlinkre-touhidalam69-linqjs-tutorial-02-lambda-expression-distinct-orderby-orderbydescending-thenby-thenbydescending-selectcustomizecolumn-20180625t080707126z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-06-25 08:06:51
last_update2018-06-25 08:06:51
depth1
children0
last_payout2018-07-02 08:06:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length753
author_reputation19,266,807,595,513
root_title"Linqjs Tutorial #02 - Lambda Expression (Distinct, OrderBy, SelectCustomizeColumn, ToJSONString)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id62,159,315
net_rshares146,238,266
author_curate_reward""
vote details (1)