जावास्क्रिप्ट ऐरे नक्शा ()
उदाहरण
सभी तत्व मानों के वर्गमूल के साथ एक नया सरणी लौटाएं:
const numbers = [4, 9, 16, 25];
const newArr = numbers.map(Math.sqrt)
सरणी में सभी मानों को 10 से गुणा करें:
const numbers = [65, 44, 12, 4];
const newArr = numbers.map(myFunction)
function myFunction(num) {
return num * 10;
}
नीचे और अधिक उदाहरण।
परिभाषा और उपयोग
map()
प्रत्येक सरणी तत्व के लिए फ़ंक्शन को कॉल करने से एक नई सरणी बनाता है।
map()
एक सरणी में प्रत्येक तत्व के लिए एक बार फ़ंक्शन को कॉल करें।
map()
खाली तत्वों के लिए फ़ंक्शन निष्पादित नहीं करता है।
map()
मूल सरणी नहीं बदलता है।
वाक्य - विन्यास
array.map(function(currentValue, index, arr), thisValue)
मापदंडों
Parameter | Description |
function() | Required. A function to be 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 value undefined .A value passed to the function to be used as its this value. |
प्रतिलाभ की मात्रा
प्रकार | विवरण |
एक सारणी | प्रत्येक सरणी तत्व के लिए फ़ंक्शन के परिणाम। |
ब्राउज़र समर्थन
map()
एक ECMAScript5 (ES5) फीचर है।
ES5 (जावास्क्रिप्ट 2009) सभी ब्राउज़रों में पूरी तरह से समर्थित है:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
और ज्यादा उदाहरण
प्रत्येक व्यक्ति के लिए पूरा नाम प्राप्त करें:
const persons = [
{firstname : "Malcom", lastname: "Reynolds"},
{firstname : "Kaylee", lastname: "Frye"},
{firstname : "Jayne", lastname: "Cobb"}
];
persons.map(getFullName);
function getFullName(item) {
return [item.firstname,item.lastname].join(" ");
}