Using forEach, map, filter, and reduce In Hindi

Using forEach, map, filter, and reduce In Hindi

जावास्क्रिप्ट प्रोग्रामिंग में forEach, map, filter, और reduce आदि बिल्ट-इन ऐरे में अवेलबल फ़ंक्शन हैं। यदि आप जावास्क्रिप्ट वेबस्क्रिप्ट या  वेबपेज में आपको ऐरे डाटा टाइप को मैन्युअल कस्टमाइज और कण्ट्रोल करना है. तो ये फंक्शन प्रोग्रामर को for या while जैसे ट्रेडिशनल लूप का उपयोग किये बिना अधिक फंक्शनल और एक्सप्रेशन आर्डर में ऐरे एलिमेंट को प्रोसेस या डील करने में हेल्प करते हैं।

Using forEach, map, filter, and reduce In Hindi

So, let’s learn more about the built-in functions like forEach, map, filter, and reduce in JavaScript programming.

forEach() JavaScript Function.

जावास्क्रिप्ट प्रोग्राम में forEach() फंक्शन मेथड का उपयोग दिए गए प्रोग्राम ऐरे में स्टार्ट टू एन्ड ऐरे एलिमेंट को रेपीटशन करने और प्रत्येक ऐरे एलिमेंट में मौजूद फ़ंक्शन को एक्सेक्यूट करने में किया जाता है। जहा forEach() फंक्शन कोई भी रिटर्न नहीं करता है, यहाँ forEach() फंक्शन का उपयोग साइड इफ़ेक्ट में किया जाता है. जैसे, जावास्क्रिप्ट प्रोग्राम में लॉगिंग या एक्सटर्नल वेरिएबल को कस्टमाइज करना है।

forEach() function syntax.

array.forEach(function(element, index, array) {

// Here is the code to execute for each element in the current array

});

forEach() function description.

  • element – यह मौजूदा ऐरे में वर्तमान आइटम को इंडीकेट करता है।
  • index (optional) – यहाँ forEach() में करंट ऐरे आइटम का इंडेक्स लोकेशन है।
  • array (optional) – यह ऐरे एलिमेंट होते है, जिसमे ऐरे एलिमेंट को उपयोग किया जा रहा है।

forEach() function example.

let integer = [3, 4, 7, 8, 9];

integer.forEach(function(int) {

console.log(int * 3);

});

forEach() program explanation.

यहाँ ऊपर प्रोग्राम में forEach() फंक्शन इन्टिजर ऐरे एलिमेंट में हर ऐरे एलिमेंट को स्टार्ट टू एंड रिपीट करता है, और इसे 3 नंबर से मल्टिप्लाय करता है।

Important notes of forEach() function.

जावास्क्रिप्ट ऐरे में forEach() फंक्शन को ब्रेक या इमीडियेट एग्जिट नहीं किया  जा सकता है, इसका मतलब है की, forEach() हमेशा कम्पलीट ऐरे एलिमेंट को रिपीट कर प्रोसेस करता है।

forEach() फंक्शन का उपयोग मुख्य रूप से ऐरे डेटा को रिप्लेस करने या फ़िल्टर करने के बजाय साइड इफ़ेक्ट के रूप में उपयोग किया जाता है।

map() JavaScript function.

किसी भी जावास्क्रिप्ट प्रोग्राम में map() फंक्शन मेथड बेसिक ऐरे में हर एलिमेंट में एक फ़ंक्शन अप्लाई करके एक नई ऐरे एलिमेंट को क्रिएट करता है। map() फंक्शन आपको एक नई ऐरे रिटर्न करता है, और ओरिजिनल ऐरे एलिमेंट को मॉडिफाई नहीं करता है।

Syntax of map() function.

let newArray = array.map(function(element, index, array) {

//Here is the code to execute each array element, returning a modified value

});

Description of map() function.

  • element – यह ऐरे में करंट ऐरे आइटम है।
  • index (optional) – यह उपयग होने वाले करंट ऐरे आइटम का इंडेक्स लोकेशन है।
  • array (optional) – यह जिस ऐरे एलिमेंट को प्रोसेस या रिपीट किया जा रहा है।

Example of map() function.

let integer = [3, 4, 7, 9, 12];

let tripleinteger = integer.map(function(int) {

return int * 3;

});

console.log(tripleinteger);

Explanation of map() function.

इस प्रोग्राम में map() tripleinteger नाम से एक नई ऐरे क्रिएट करता है, जहाँ हर ऐरे एलिमेंट ओरिजनल ऐरे से रिलेटेड एलिमेंट को 3 से मल्टिप्लाय करके रिजल्ट प्रीव्यू करता है।

Important notes of map() function.

जावास्क्रिप्ट प्रोग्राम में map() फंक्शन हमेशा आपको आउटपुट में एक नई ऐरे रिटर्न  करता है, जहा आपकी ओरिजिनल ऐरे एलिमेंट अपरिवर्तित रहते है।

जावास्क्रिप्ट में map() फंक्शन का उपयोग किसी ऐरे के एलिमेंट को मॉडिफाई करने में हेल्पफुल है।

filter() JavaScript Function.

जावास्क्रिप्ट प्रोग्राम में फ़िल्टर() फंक्शन मेथड सभी एलिमेंट के साथ एक नई ऐरे क्रिएट करता है, जो दिए गए फ़िल्टर() फ़ंक्शन द्वारा फंक्शन टेस्टिंग को पास करते हैं। फ़िल्टर() फंक्शन का उपयोग पर्टिकुलर कंडीशन के आधार पर अवांछित ऐरे एलिमेंट को फ़िल्टर कर प्रॉसेस करने में किया जाता है।

Syntax of filter() JavaScript Function.

let newArray = array.filter(function(element, index, array) {

// Here returns true if the array element should be added to the new array, else returns false

});

Description of filter() JavaScript Function.

  • element – यह फंक्शन में ऐरे में करंट आइटम होते है।
  • index (optional) – यह करंट ऐरे आइटम का इंडेक्स लोकेशन है।
  • array (optional) – यह मौजूदा ऐरे में जिस ऐरे एलिमेंट को स्टार्ट टू एन्ड प्रोसेस किया जा रहा है।

Example of filter() JavaScript Function.

let integer = [11, 14, 21, 22, 90, 81, 100];

let eveninteger = integer.filter(function(int) {

return int % 2 === 0;

});

console.log(eveninteger);

Explanation of filter() function.

filter() फंक्शन जावास्क्रिप्ट प्रोग्राम में एक नई ऐरे eveninteger क्रिएट करता है, जहा केवल ओरिजिनल ऐरे एलिमेंट में सम इन्टिजर वैल्यू होते हैं।

Important notes of filter() function.

यहाँ filter() फंक्शन जावास्क्रिप्ट प्रोग्राम में एक नई ऐरे रिटर्न करता है, जिसमें केवल वे ऐरे एलिमेंट होते हैं, जो दी गई कंडीशन को कम्पलीट करते हैं।

यदि filter() फंक्शन में कोई ऐरे एलिमेंट कंडीशन को कम्पलीट नहीं करता है, तो यह एक एम्प्टी ऐरे को रिटर्न करता है।

reduce() JavaScript function.

reduce() फंक्शन मेथड ऐरे के हर एलिमेंट को (बाएं से दाएं) एक रिड्यूसर फ़ंक्शन करता है, जो प्रोग्रामर अप्लाई करते हैं, उसे एक्सेक्यूट करता है, आउटपुट रिजल्ट को स्टोर करती है। और यह एक सिंगल वैल्यू को रिटर्न करता है।

Syntax of reduce() JavaScript function.

let result = array.reduce(function(accumulator, currentValue, index, array) {

//This returns the updated accumulator value after processing the current array element

}, initialValue);

Description of reduce() function.

  • Accumulator – हर ऐरे रेपीटशन के बाद रिटर्न किया गया संचित वैल्यू है, यह वैल्यू कुछ भी हो सकती है:, यह एक नंबर, ऑब्जेक्ट, ऐरे, आदि एलिमेंट में हो सकती है।
  • currentValue – यह मौजूदा प्रोग्राम में संसाधित किया जा रहा करंट ऐरे आइटम है।
  • index (optional) – यह करंट ऐरे आइटम का इंडेक्स लोकेशन है।
  • array (optional) – यह प्रोसेस की जाने वाली ऐरे एलिमेंट है।
  • initialValue (optional) – यह संचायक के लिए इनिशियल वैल्यू है, यदि यह वैल्यू प्रदान नहीं की गई है, तो ऐरे का पहला एलिमेंट इनिशियल वैल्यू के रूप में यूज़ किया गया है.

Example of reduce() JavaScript function.

let integer = [7, 11, 8, 9, 10];

let total = integer.reduce(function(acc, int) {

return acc + int;

}, 0);

console.log(total);

Explanation of reduce() JavaScript function.

यहाँ मौजूदा प्रोग्राम में reduce() फंक्शन ऐरे में सभी इन्टिजर का टोटल करता है।

जहा acc (संचायक) 0 से स्टार्ट होता है, और ऐरे में हर इन्टिजर के लिए, acc को करंट इन्टिजर में ऐड कर अपडेट करता है।

Example using reduce() to find the maximum value.

let integer = [0, 3, 7, 400, 234];

let max = integer.reduce(function(acc, int) {

return int > acc ? int : acc;

}, integer[0]);

console.log(max);

Explanation of reduce() JavaScript function.

ऊपर दिए गए प्रोग्राम में reduce() फंक्शन का उपयोग है की, यहाँ हर इन्टिजर नंबर की तुलना करने और मैक्सिमम वैल्यू को फाइंड कर डिस्प्ले करने में किया जाता है।

Important Notes of reduce() JavaScript Function.

जावास्क्रिप्ट प्रोग्राम में reduce() फंक्शन का उपयोग मल्टी पर्पस के लिए किया जा सकता है, जैसे वैल्यू का टोटल करना, मैक्सिमम /मिनिमम वैल्यू को फाइंड करना, या यहाँ तक कि ऐरे एलिमेंट को समतल करना आदि है।

reduce() में initialValue यह सुनिश्चित करने के लिए अति महत्वपूर्ण है कि, ऐरे में पहला एलिमेंट प्रॉपर्ली हैंडल किया गया है, स्पेशली, जब टोटल जैसे ऑपरेशन के लिए है।

Detail about forEach, map, filter, and reduce function.

FunctionReturns typeModifies Original Array or notWhy use
forEach()Used in array to undefined (side effects) array elementNoIt used in array To perform an action for each array element (e.g., logging, updating external variables).
map()It used to New array of transformed elementsNoIt used in array To transform the elements into a new array element.
filter()It used to New array of filtered elementsNoIt used to filter elements based on a given program condition.
reduce()It used to A single value (e.g., sum, object)NoTo accumulate or reduce all array elements into one array value.

Conclusion of forEach, map, filter, and reduce.

जावास्क्रिप्ट प्रोग्राम में forEach() फंक्शन का उपयोग तब करें, जब आपको जावास्क्रिप्ट प्रोग्राम में साइड इफ़ेक्ट के लिए ऐरे एलिमेंट में रेपीटशन करने की आवश्यकता हो।

जावास्क्रिप्ट प्रोग्राम में map() फंक्शन का उपयोग तब करें, जब आपको ऐरे के एलिमेंट  को एक नई ऐरे में कन्वर्ट करने की आवश्यकता हो।

जावास्क्रिप्ट प्रोग्राम में filter() फंक्शन का उपयोग तब करें, जब आपको किसी पर्टिकुलर कंडीशन के आधार पर ऐरे एलिमेंट का चयन करने और एक नई ऐरे क्रिएट करने की आवश्यकता हो।

जावास्क्रिप्ट प्रोग्राम में reduce() फंक्शन का उपयोग तब करें, जब आपको सिंगल आउटपुट में वैल्यूज को संचित करने की जरूरत हो।

Leave a Reply