As I said in the series : What programming language to learn now ? Current Javascript is becomming a new movement . Recently , because I am learning Node.js yourself so I decide should focus on Javascript research more . So during this time I will post more articles related to Javascript , hope you follow .

As you know about LINQ , you have seen the robustness  and usability of LINQ in C# ( Java 8 just updated the Stream API , but still loses much of LINQ … ) . In this article , I will introduce some of the functions ( less people know ) available in the prototype Array of Javascript , also has the same effect as LINQ .

advanced-javascript-3-638

For simplicity , for every function  on Java, I would give the same function in C# . I will use the following list to illustrate the function .

var students = new List<Student>()

 {

 new Student() {Name = "Hoang", Age = 15},

 new Student() {Name= "Minh", Age= 5},

 new Student() {Name= "Sang", Age= 50},

 new Student() {Name= "Long", Age= 27},

 new Student() {Name= "Khoa", Age= 10},

 };

____________________________________________________________________________________________

var students=[

  {Name: 'Hoang', Age: 20},

  {Name: 'Minh', Age: 5},

  {Name: 'Sang', Age: 50},

  {Name: 'Long', Age: 27},

  {Name: 'Khoa', Age: 10},

];

1. filter

In case you want to filter some of the elements in the list , we usually use the Where function in LINQ . In Javascript , we use the filter function . This function needs to pass a function to the callback , with three parameters : element , index , array .

var ageLess20 = students.Where(st => st.Age < 20);

____________________________________________________________________________________________

var ageLess20 = students.filter(function(element, index, array){

      return element.Age > 22;

 });

 //If dont care about index or array , we could code 

 var ageLess20 = students.filter(function(st){ return st.Age > 22; });

 //On the Firefox, current Javascript updated version ES6

//We can use arrow function, same lambda expression

var ageLess20 = students.filter(st -> st.Age > 22);

2. every và some

In LINQ , we often use the All function to test all elements satisfying one condition , Any function to test if at least one element satisfies one condition . In Javascript , the two functions we use are ‘every’ and ‘some’
//Check all students great than 5 year old 

 boolallStudentLarger5 = students.All(st => st.Age > 5); // false

 boolanyStudentAge50 = students.Any(st => st.Age == 50); // true
____________________________________________________________________________________________
varallStudentLarger5 = students.every(function(st){ returnst.Age > 5 }); // false

 varanyStudentAge50 = students.some(function(st){ returnst.Age == 50 }); // true

3. forEach

This function will browse through all the parts of the list , and call function passed .
//Browse sequence one by one student in the list , print out 

students.ForEach(Console.WriteLine);
____________________________________________________________________________________________
//With javascript

students.forEach(console.log);

4. map

In LINQ , sometime , we need filter out some fields of objects in the list , we often use ‘Select’ . In Javascript , the same function is used as a map function .
//Filter one list include student's name 

var names = students.Select(st => st.Name);

//["Hoang", "Minh", "Sang", "Long", "Khoa"]
____________________________________________________________________________________________
varnames = students.map(function(st){ returnst.Name });

//['Hoang', 'Minh', 'Sang', 'Long', 'Khoa']

5. reduce

This is an alternative function to the Aggregate in LINQ . This operator is a bit hard to explain , please read more here : http://stackoverflow.com/questions/7105505/linq-aggregate-algorithm-explained

6. find và findIndex (just appear firefox and safari)

To find an element in the list , with LINQ we often use FirstOrDefault ( to get that element itself ) , or FindIndex ( to find index of that element in the list ) . Note that these two functions are in beta , only run on firefox and safari .
//find student have age equal 50
Student found = students.FirstOrDefault(st => st.Age == 50);
//Student Long, Age = 50
//find that student's index 
intindex = students.FindIndex(st => st.Age == 50); //2
____________________________________________________________________________________________
//Just run on firefox and safari
var found = students.find(function(st){ return st.Age = 50 });
var index = students.findIndex(function(st){ return st.Age = 50 });
If you find these functions do not meet the requirements, wait for the following article, I will introduce lo-dash, a library with countless functions to support the browsing and processing of the array. I guarantee after familiar use, you will not be able to code javascript without it. (Like 90% C # developers can not live without LINQ right now.)
Conclusion: In personal opinion, Javascript is just like a girl. Looking outside, it looks like she’s a pretty hot girl, lots of people chasing (1 batch of JavaScript frameworks written for javascript). New acquaintance, again found her quite confused, complex, sometimes quite crazy, sometimes quite easy (It is easy because sometimes code js wrong to say it is not crazy, but crazy about Js then you should google to learn more: http://webreflection.blogspot.com/2012/10/javascript-made-everyone-crazy.html ). There are things, after having used a time to find her very cute, handy, what also know. Narrow down, please take this picture to finish the article.
Cái nhìn của tác giả về javascript: Xưa và nay.
source : https://techtalk.vn/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s