जावास्क्रिप्ट ऐरे कम करता है () विधि सरणी के प्रत्येक तत्व पर एक reducer फ़ंक्शन निष्पादित करता है और इसे एक संचायक के खिलाफ लागू करता है।
reduceRight()
विधि का सिंटैक्स है:
arr.reduceRight(callback(accumulator, currentValue), initialValue)
यहाँ, अरै एक अरै है।
घटाई () पैरामीटर
reduceRight()
विधि में लेता है:
- कॉलबैक - प्रत्येक सरणी तत्व पर निष्पादित करने के लिए फ़ंक्शन। इसमें निम्न है:
- संचायक - यह कॉलबैक के रिटर्न मूल्यों को जमा करता है। यह है
initialValue
अगर आपूर्ति की पहली कॉल के लिए, - currentValue - वर्तमान तत्व सरणी से पास किया जा रहा है।
- संचायक - यह कॉलबैक के रिटर्न मूल्यों को जमा करता है। यह है
- initialValue (वैकल्पिक) - ऐसा मान जो
callback()
पहले कॉल पर पास किया जाएगा । यदि प्रदान नहीं किया गया है, तो अंतिम तत्व पहले कॉल पर संचायक के रूप में कार्य करता है औरcallback()
उस पर निष्पादित नहीं होगा।
नोट:reduceRight()
initialValue के बिना खाली सरणी पर कॉल करना फेंक देगा TypeError
।
कम कीमत से वापसी ()
- सरणी को कम करने के बाद परिणाम लौटाता है।
नोट :
reduceRight()
प्रत्येक मान के लिए दिए गए फ़ंक्शन को दाईं से बाईं ओर निष्पादित करता है।reduceRight()
मूल सरणी नहीं बदलता है।- यह प्रदान करने के लिए लगभग हमेशा सुरक्षित है
initialValue
।
उदाहरण 1: सरणी के सभी मूल्यों का योग
const numbers = (1, 2, 3, 4, 5, 6); function sum_reducer(accumulator, currentValue) ( return accumulator + currentValue; ) let sum = numbers.reduceRight(sum_reducer); console.log(sum); // 21 // using arrow function let summation = numbers.reduceRight( (accumulator, currentValue) => accumulator + currentValue ); console.log(summation); // 21
आउटपुट
२१ २१
उदाहरण 2: ऐरे में संख्या घटाना
const numbers = (50, 300, 20, 100, 1800); // subtract all numbers from last number // since 1st element is called as accumulator rather than currentValue // 1800 - 100 - 20 - 300 - 50 let difference = numbers.reduceRight( (accumulator, currentValue) => accumulator - currentValue ); console.log(difference); // 1330 const expenses = (1800, 2000, 3000, 5000, 500); const salary = 15000; // function that subtracts all array elements from given number // 15000 - 500 - 5000 - 3000 - 2000 - 1800 let remaining = expenses.reduceRight( (accumulator, currentValue) => accumulator - currentValue, salary ); console.log(remaining); // 2700
आउटपुट
1330 2700
यह उदाहरण स्पष्ट रूप से एक initialValue पास करने और एक initialValue पास न करने के बीच के अंतर को स्पष्ट करता है।
उदाहरण 3: समग्र कार्य बनाएँ
// create composite functions const composite = (… args) => (initialArg) => args.reduceRight((acc, fn) => fn(acc), initialArg); const sqrt = (value) => Math.sqrt(value); const double = (value) => 2 * value; const newFunc = composite(sqrt, double); // ( 32 * 2 ) ** 0.5 let result = newFunc(32); console.log(result); // 8
आउटपुट
।
हम जानते हैं कि फ़ंक्शन रचना वह तरीका है जिसमें एक फ़ंक्शन से परिणाम दूसरे फ़ंक्शन को पास किया जाता है। निष्पादन दाएं से बाएं होता है, इसलिए हम reduceRight()
फ़ंक्शन का लाभ उठा सकते हैं ।
इस उदाहरण में, हमने एक composite()
फ़ंक्शन बनाया है जो एक मनमानी संख्या में तर्क लेता है। यह फ़ंक्शन एक और फ़ंक्शन देता है जो initialArg
इसमें दिए गए कार्यों के खिलाफ दाएं से बाएं ओर लागू करके इस मूल्य को कम कर देता है।
अनुशंसित पढ़ना: जावास्क्रिप्ट ऐरे को कम करें ()