简体   繁体   English

jquery中filter和map之间的区别是什么

[英]what is the difference between filter and map in jquery

What is the difference between map() and filter() in jQuery? jQuery中map()filter()什么区别?

Kindly differentiate between the two in simple words. 请用简单的词来区分这两者。

You've asked about map and filter "in jQuery." 你已经问过“在jQuery中”的mapfilter jQuery does have map and filter , and separately , JavaScript arrays have map and filter . jQuery的确实有mapfilter ,并单独 ,JavaScript数组已mapfilter Their purposes and uses are similar, although sadly the information they give their callbacks is different. 他们的目的和用途是相似的,尽管可悲的是他们给回调的信息是不同的。

filter is for taking a jQuery object or array and producing a new jQuery object or array that only contains a subset of the entries in the original — that is, we apply a filter to it, getting only the entries that match the filter. filter用于获取jQuery对象或数组,并生成一个新的jQuery对象或数组,该对象或数组仅包含原始条目的子集 - 也就是说,我们对其应用过滤器,仅获取与过滤器匹配的条目。 So the result will be the same size (if you don't remove anything) or smaller, but never larger, and will only have entries from the original. 因此结果将是相同的大小(如果你不删除任何东西)或更小,但从不大,并且只有原始条目。

map is for taking a jQuery object or array and producting a new jQuery object or array with new, different entries based on the entries in the original. map用于获取jQuery对象或数组,并根据原始条目生成具有新的不同条目的新jQuery对象或数组。 The result will always be the same size as the original, but the entries in the result could be completely different from the original's. 结果将始终与原始大小相同,但结果中的条目可能与原始条目完全不同。

So as you can see, they do different things: filter produces a subset of the entries from the original, while map produces a result of the same size as the original but with (potentially) completely different contents. 正如您所看到的,它们执行不同的操作: filter生成原始条目的子集,而map生成的结果与原始大小相同但具有(可能)完全不同的内容。

Both of them can use callbacks ( map always does). 它们都可以使用回调( map总是这样)。 What those callbacks do is very different, though: filter 's callback decides whether to keep the entry: It returns a flag where any truthy* value means "keep the entry," and any falsy* value means "remove it." 这些回调的作用是非常不同的: filter的回调决定是否保留条目:它返回一个标志,其中任何truthy *值表示“保留条目”,任何falsy *值表示“删除它”。 But map 's callback returns the actual entry to use in the resulting jQuery object or array. map的回调返回在生成的jQuery对象或数组中使用的实际条目。

You've asked specifically about jQuery, so let's take a jQuery example to start with, see comments: 你已经具体询问了jQuery,所以让我们先来看一个jQuery示例,看看评论:

 // Let's get all the divs inside our #example div var divs = $("#example div"); console.log("total divs: " + divs.length); // `filter` lets us take that set of elements and filter out some; // let's keep only the ones with the class `b`, using the ability // of jQuery's `filter` to accept a CSS selector var withb = divs.filter(".b"); console.log("divs with class b: " + withb.length); // We could have used a callback function instead: var withb2 = divs.filter(function() { // We return a "truthy" value to keep this entry, or a "falsy" // one to remove it return $(this).hasClass("b"); }); console.log("divs with class b: " + withb2.length); // `map` lets us create a new jQuery object with completely different // contents, where each entry is derived from the original entry. // Let's get a jQuery object containing the `data-foo` value of each // entry in our divs. The result will be the same size as the original, // but will have entirely different contents. var foos = divs.map(function() { return $(this).attr("data-foo"); }); console.log("foos.length: " + foos.length); console.log("foos: " + foos.get().join(", ")); 
 <div id="example"> <div data-foo="1" class="ab">I'm .ab</div> <div data-foo="2" class="a">I'm .a</div> <div data-foo="3" class="b">I'm .b</div> <div data-foo="4" class="ab">I'm .ab</div> <div data-foo="5" class="a">I'm .a</div> <div data-foo="6" class="b">I'm .b</div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Notice how map let us create a jQuery object whose contents were not DOM elements. 注意map如何让我们创建一个内容不是DOM元素的jQuery对象。 99.999% of the time, jQuery objects contain DOM elements and most jQuery methods expect that the object contains DOM elements. 99.999%的时间,jQuery对象包含DOM元素,大多数jQuery方法期望对象包含DOM元素。 But you can create jQuery objects with other things (like our strings above from data-foo ). 但是你可以用其他东西创建jQuery对象(比如data-foo上面的字符串)。 Typically it's rare, and you almost always end up using get() on the result to get a plain array of those values. 通常它很少见,而且你几乎总是在结果上使用get()来获得这些值的普通数组。

Normally with jQuery's map and filter , you use this within the callback. 通常使用jQuery的mapfilter ,您可以在回调中使用this It refers to the current entry in the jQuery object. 它引用jQuery对象中的当前条目。 They also receive arguments such as the index of the entry in the jQuery object, see the documentation for details. 它们还接收参数,例如jQuery对象中条目的索引,有关详细信息,请参阅文档。 Note that the arguments jQuery passes its map and filter functions are different from the arguments JavaScript passes its map and filter functions. 请注意,jQuery传递其mapfilter函数的参数与JavaScript传递其mapfilter函数的参数不同

Speaking of which... 说到哪......


JavaScript arrays also have map and filter . JavaScript数组也有mapfilter Conceptually they do the same thing as jQuery's. 从概念上讲,它们与jQuery的功能相同。 Let's take a look: 让我们来看看:

 // Say I have var array = [1, 2, 3, 4, 5, 6]; // I can use `filter` to get an array of just odd numbers: var odds = array.filter(function(entry) { return entry % 2 == 1; }); console.log("odds: " + odds); // I can use `map` to get an array of those numbers doubled: var doubles = array.map(function(entry) { return entry * 2; }); console.log("doubles: " + doubles); 


* "truthy" and "falsy": A value that coerces to true when used as a boolean is a "truthy" value; *“truthy”和“falsy”:当用作布尔值时强制为true的值是“truthy”值; one that coerces to false is a "falsy" one. 一个强迫false是一个“虚假的”。 The falsy values in JavaScript are 0 , NaN , null , undefined , "" , and of course, false . JavaScript中的虚假值是0NaNnullundefined"" ,当然还有false All other values are truthy. 所有其他值都是真实的。

filter() is used to skip unwanted elements of collection. filter()用于跳过不需要的集合元素。 map() is used to modify elements of collection. map()用于修改集合的元素。

filter() may return collection with less elements then in original collection. filter()可以返回原始集合中包含较少元素的集合。 It may even return empty collection. 它甚至可能返回空集合。 map() will always return collection with the same number of elements. map()将始终返回具有相同数量元素的集合。

Both filter() and map() will require as an argument a function that do something with single element of collection. filter()map()都需要一个函数来处理集合的单个元素。

What is different is that filter() expects that function to return Boolean value based on which element will be skipped or not. 不同的是filter()期望该函数根据将跳过哪个元素返回布尔值。 map() will expect that function to return an new collection element. map()将期望该函数返回一个新的集合元素。

filter() , map() and quite a few others (eg reduce() ) exist because of observation that a lot of collection manipulating algorithms can be split into two separate behaviors. filter()map()和相当多的其他(例如reduce() )存在是因为观察到许多集合操作算法可以分成两个独立的行为。 One is doing something to element, and other is making sure that something is done to all elements in a collection. 一个是对元素做一些事情,另一个是确保对集合中的所有元素进行某些操作。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM