Javasctipt Array LINQ Help Class

Source: Internet
Author: User
Tags bool new set

This problem is often encountered in the schedule. A JS array we need to find some of the required class capacity

Or the sum of the class capacity and the average in the array, the general practice is to add the class tolerance in the loop to a new set.

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);
    }
 }

If there's more code to do the query, we'll have to write a lot of loops.

Recall that the list and array in C # are similar but System.Linq.Enumerable extended classes give him a lot of Linq extension methods

So that we can query the list like the following code

list<int> list = new list<int> ();
        A.add (1);
        A.add (2);
        A.add (3);
        A.add (4);
        A.add (5);
        A.add (6);
         list<int> copylist = list. Where (model => model < 3);

Can I get javasctipt to make an extension class that's about the same as System.Linq.Enumerable? The answer is yes. Let's define a JavaScript script like the following

Array.prototype.Where = function (func) {
                    var copy = [];
                    for (Var model in this) {
                        try {
                            if (func (model)) {
                                Copy.push (model);
                            }
                        }
                        catch (ex) {}}}
                

Put it at the top of the page. We'll be pleasantly surprised to find out that Array has a more than one where method Javascript each class method adds a method to the prototype and adds a method to it.

Let's call the test.

var array = [1,2,3,4,5,6];

var Copyarray = array. Where (function (model) {return model < 1;})

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

We'll find that this code has the same effect as the first piece of code, but it's going to get a lot less code.

So we can define the common methods in other System.Linq.Enumerable.

(function () {/* Filters the 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} retur
                             
                        N 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 element in the collection that meets the requirements Func: (e) =>{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 each use of the definition of the file directly to the storage of JS files on the page every time you can use these extension methods

And one thing is that Javasctipt's method annotation is such a/**/, not/

The difference is that there's a hint in the VS IDE.

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.