Thursday, May 19, 2011

jQuery's LINQ's Select

<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>

<body>

@{
    var a = new[] { "The", "quick", "brown", "fox" };
    
    

    var b = a.Select(v => "www." + v + ".com");
    
    foreach(var t in b) {
        Response.Write(t + "<br/>");
    }

    int i = 0;
    var c = b.Select(v => new { v, i = ++i, m = i * 2 });
    
    foreach(var t in c) {
        Response.Write(t.v + " xxx " + t.i + " yyy " + t.m + "<br/>");
    }
    
}



<hr />


<script>

    // This evil code was sourced from http://stackoverflow.com/questions/761148/jquery-document-ready-and-document-write/761190#761190
    $(function () {
        document.write = function (evil) {            
            $('body').append(evil);            
        }
    });
    // ...evil :p mwahahah


    $(function () {
       
        a = ["jumps", "over", "lazy", "dog"];

        b = $.map(a, function (v) {
            return "www." + v + ".com";
        });

        $.each(b, function () {
            document.write(this + "<br/>");
        });


        i = 0;
        c = $.map(b, function (v) {
            return { v: v, i: ++i, m: i * 2 };
        });

        $.each(c, function () {
            document.write(this.v + " xxx " + this.i + ' yyy ' + this.m + "<br/>");
        });

    });
</script>


</body>


Output:

www.The.com
www.quick.com
www.brown.com
www.fox.com
www.The.com xxx 1 yyy 2
www.quick.com xxx 2 yyy 4
www.brown.com xxx 3 yyy 6
www.fox.com xxx 4 yyy 8

www.jumps.com
www.over.com
www.lazy.com
www.dog.com
www.jumps.com xxx 1 yyy 2
www.over.com xxx 2 yyy 4
www.lazy.com xxx 3 yyy 6
www.dog.com xxx 4 yyy 8

No comments:

Post a Comment

Post a Comment