प्रत्येक के लिए जावास्क्रिप्ट ऐरे ()
उदाहरण 1
फलों में प्रत्येक तत्व के लिए एक फ़ंक्शन को कॉल करें:
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
परिभाषा और उपयोग
विधि एक सरणी में प्रत्येक तत्व के लिए एक फ़ंक्शन को forEach()
कॉल करती है।
forEach()
खाली तत्वों के लिए विधि निष्पादित नहीं की जाती है।
वाक्य - विन्यास
array.forEach(function(currentValue, index, arr), thisValue)
मापदंडों
function() | Required. A function to run for each array element. |
currentValue | Required. The value of the current element. |
index | Optional. The index of the current element. |
arr | Optional. The array of the current element. |
thisValue | Optional. Default undefined .A value passed to the function as its this value. |
प्रतिलाभ की मात्रा
अपरिभाषित |
ब्राउज़र समर्थन
forEach()
एक ECMAScript5 (ES5) फीचर है।
ES5 (जावास्क्रिप्ट 2009) सभी ब्राउज़रों में पूरी तरह से समर्थित है:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
और ज्यादा उदाहरण
योग की गणना करें:
let sum = 0;
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction);
function myFunction(item) {
sum += item;
}
प्रत्येक तत्व को गुणा करें:
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)
function
myFunction(item, index, arr) {
arr[index] = item * 10;
}