मैंने थोड़ी देर पहले यह जवाब लिखा था, और मेरी राय बदल गई है। मैं अपने ब्लॉग पोस्ट की जांच करने की अनुशंसा करता हूं जो इस विषय को फैलाता है और यह बहुत बेहतर बताता है। यह विकल्प के अंत में एक जेएसपीआरएफ तुलना भी देता है।
The tl;dr is this:
To accomplish what you're asking for (filtering and mapping within one function call), you should use Array.reduce()
. However, the more readable and usually faster approach is to just use filter and map chained together:
[1,2,3].filter(num => num > 2).map(num => num * 2)
निम्नानुसार है कि कैसे Array.reduce()
काम करता है, और एक पुनरावृत्ति में फ़िल्टर और मानचित्र को पूरा करने के लिए इसका उपयोग कैसे किया जा सकता है। यदि यह बहुत संघनित है, तो मैं ऊपर से जुड़े ब्लॉग पोस्ट को देखने की अत्यधिक अनुशंसा करता हूं, जो स्पष्ट उदाहरणों और प्रगति के साथ एक और अधिक अनुकूल परिचय है।
आप एक तर्क को कम करते हैं जो एक (आमतौर पर अज्ञात) फ़ंक्शन होता है।
That anonymous function takes two parameters--one (like the anonymous functions passed in to map/filter/forEach) is the iteratee to be operated on. There is another argument for the anonymous function passed to reduce, however, that those functions do not accept, and that is the value that will be passed along between function calls, often referred to as the memo.
ध्यान दें कि जबकि Array.filter() केवल एक तर्क (एक फ़ंक्शन) लेता है, Array.reduce() भी एक महत्वपूर्ण (हालांकि वैकल्पिक) दूसरा तर्क लेता है: 'ज्ञापन' के लिए प्रारंभिक मान जिसे उस अज्ञात फ़ंक्शन में पारित किया जाएगा पहला तर्क, और बाद में फंक्शन कॉल के बीच उत्परिवर्तित और पारित किया जा सकता है। (यदि यह आपूर्ति नहीं की जाती है, तो पहले अज्ञात फ़ंक्शन कॉल में 'ज्ञापन' डिफ़ॉल्ट रूप से पहला इटेटेट होगा, और 'iteratee' तर्क वास्तव में सरणी में दूसरा मान होगा)
हमारे मामले में, हम शुरू करने के लिए एक खाली सरणी में गुजरेंगे, और फिर चुनें कि हमारे एरेरेट को हमारे सरणी में इंजेक्ट करना है या हमारे फ़ंक्शन के आधार पर नहीं - यह फ़िल्टरिंग प्रक्रिया है।
अंत में, हम प्रत्येक अज्ञात फ़ंक्शन कॉल पर हमारी 'सरणी प्रगति' वापस कर देंगे, और कम करने के लिए उस वापसी मूल्य को ले जाएगा और इसे अगले कार्य कॉल में एक तर्क (ज्ञापन कहा जाता है) के रूप में पास कर देगा।
यह फिल्टर और मानचित्र को एक पुनरावृत्ति में होने की अनुमति देता है, जिससे आधे में आवश्यक पुनरावृत्तियों की संख्या कम हो जाती है। :)
अधिक पूर्ण स्पष्टीकरण के लिए, MDN या उपरोक्त लिंक। :)
कम करें कॉल का मूल उदाहरण:
let array = [1,2,3];
const initialMemo = [];
array = array.reduce((memo, iteratee) => {
//if condition is our filter
if (iteratee > 1) {
//what happens inside the filter is the map
memo.push(iteratee * 2);
}
//this return value will be passed in as the 'memo' argument
//to the next call of this function, and this function will have
//every element passed into it at some point.
return memo;
}, initialMemo)
console.log(array)//[4,6], equivalent to [(2 * 2), (3 * 2)]
अधिक संक्षिप्त संस्करण:
[1,2,3].reduce((memo, value) => value > 1 ? memo.concat(value * 2) : memo, [])
ध्यान दें कि पहला iteratee एक से अधिक नहीं था, और इसलिए फ़िल्टर किया गया था। इसके प्रारंभिक मेमो को भी ध्यान दें, जिसका नाम सिर्फ अस्तित्व को स्पष्ट करने और इसे ध्यान में रखने के लिए किया गया है। एक बार फिर, इसे पहले ज्ञात फ़ंक्शन कॉल में 'ज्ञापन' के रूप में पारित किया जाता है, और उसके बाद अनाम फ़ंक्शन का लौटा मूल्य अगले कार्य में 'ज्ञापन' तर्क के रूप में पारित किया जाता है।
ज्ञापन के लिए क्लासिक उपयोग केस का एक और उदाहरण एक सरणी में सबसे छोटी या सबसे बड़ी संख्या लौटाएगा। उदाहरण:
[7,4,1,99,57,2,1,100].reduce((memo, val) => memo > val ? memo : val)
// ^this would return the largest number in the list.
अपना खुद का कम करने के तरीके को लिखने का एक उदाहरण (यह अक्सर इस तरह के कार्यों को समझने में मदद करता है, मुझे लगता है):
test_arr = [];
// we accept an anonymous function, and an optional 'initial memo' value.
test_arr.my_reducer = function(reduceFunc, initialMemo) {
//if we did not pass in a second argument, then our first memo value
//will be whatever is in index zero. (Otherwise, it will
//be that second argument.)
const initialMemoIsIndexZero = arguments.length < 2;
//here we use that logic to set the memo value accordingly.
let memo = initialMemoIsIndexZero ? this[0] : initialMemo;
//here we use that same boolean to decide whether the first
//value we pass in as iteratee is either the first or second
//element
const initialIteratee = initialMemoIsIndexZero ? 1 : 0;
for (var i = initialIteratee; i < this.length; i++) {
//memo is either the argument passed in above, or the
//first item in the list. initialIteratee is either the
//first item in the list, or the second item in the list.
memo = reduceFunc(memo, this[i]);
}
//after we've compressed the array into a single value,
//we return it.
return memo;
}
असली कार्यान्वयन इंडेक्स जैसी चीजों तक पहुंच की इजाजत देता है, उदाहरण के लिए, लेकिन मुझे उम्मीद है कि इससे आपको इसके बारे में एक जटिल अनुभव मिल जाएगा।