Some simple help classes (2) -- JAVASCTIPT ARRAY LINQ

Source: Internet
Author: User

This problem is often encountered in the schedule. a js array. We need to find out some qualified class capacities, or find the average class capacity sum in the logarithm group. The general approach is to loop. to add the class to a new set and copy the code var array = []; array. push (1); array. push (2); array. push (3); array. push (4); array. push (5); array. push (6); var whereArray = []; for (var model in array) {if (model <3) {whereArray. push (model) ;}} copy the Code. If more code needs to be queried, We have to write a lot of loops to recall that the List set in C # is similar to Array, but System. linq. the Enumerable extension class provides a lot of Linq extension methods so that we can query the List copy code List like the following code <int> list = new List <int> (); a. add (1);. add (2);. add (3);. add (4);. add (5);. add (6); List <int> copyList = list. where (model => model <3); Can I copy the code and use ctipt pt to make a file with System. linq. the extension class of Enumerable is similar. The answer is yes. we can define the following JavaScript script to copy the code Array. prototype. where = function (func) {var copy = []; for (var model in this) {try {if (func (model) {copy. push (model) ;}} catch (ex) {}}} copy the code and place it on the top of the page. We will be pleasantly surprised to find that Array has a Where method while Javascript. Each class method will add a method to it in prototype, that is, inner. we have added a method to call var array = [1, 2, 3, 4, 5, 6]; var copyArray = array. where (function (model) {return model <1 ;}) we will find that this code has the same effect as the first code, but the code needs to be reduced a lot so that we can use other systems. linq. the common methods in Enumerable are defined one by one to copy the code (function () {/* filter elements that meet the requirements func: (e) =>{ return bool} return Array */Array. prototype. where = function (func) {var copy = []; for (var model in this) {try {if (func (model) {copy. push (model) ;}} catch (ex) {}}/* sum func: (e) =>{ return Number} return Number */Array. prototype. sum = function (func) {var int = 0; for (var model in this) {try {var NaN = func (model); if (isNaN (NaN )) {a + = NaN * 1 ;}} catch (ex) {}} return int ;}/ * gets the first qualified element func: (e) in the set) => {return bool} return e or NULL */Array. prototype. find = function (func) {for (var model in this) {try {if (func (model) {return model ;}} catch (ex) {} return null}) (Array) after the code is copied and defined, the file for storing this JS file is directly dragged to the page every time you use the extension method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.