]> source.dussan.org Git - jquery.git/commitdiff
Added $(...).map() functionality. (Also closes #1250, imo)
authorJohn Resig <jeresig@gmail.com>
Fri, 31 Aug 2007 03:26:03 +0000 (03:26 +0000)
committerJohn Resig <jeresig@gmail.com>
Fri, 31 Aug 2007 03:26:03 +0000 (03:26 +0000)
src/jquery/coreTest.js
src/jquery/jquery.js

index e9192849758e6562df3a477493f7ac00d8390b44..40be7f87057c0a626179e3556a7bc26b9e3efc51 100644 (file)
@@ -976,3 +976,23 @@ test("slice()", function() {
        isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
        isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
 });
+
+test("map()", function() {
+       expect(2);
+
+       isSet(
+               $("#ap").map(function(){
+                       return $(this).find("a").get();
+               }),
+               q("google", "groups", "anchor1", "mark"),
+               "Array Map"
+       );
+
+       isSet(
+               $("#ap > a").map(function(){
+                       return this.parentNode;
+               }),
+               q("ap","ap","ap"),
+               "Single Map"
+       );
+});
index 8988c5ee107617620e249b4cd36e04cb914b08ee..8180a4ea217d0fa4076091ec527abf3c1fa7c608 100644 (file)
@@ -1189,6 +1189,12 @@ jQuery.fn = jQuery.prototype = {
        slice: function() {
                return this.pushStack( Array.prototype.slice.apply( this, arguments ) );
        },
+
+       map: function(fn){
+               return this.pushStack(jQuery.map( this, function(elem,i){
+                       return fn.call( elem, i, elem );
+               }));
+       },
        
        /**
         * @private