JavaScript arrays डेटा को रूपांतरित करने, खोजने और पुनः क्रमबद्ध करने के लिए अंतर्निहित मेथड्स का समृद्ध सेट प्रदान करते हैं। यह चीट शीट आवश्यक array methods को कार्य के अनुसार समूहित करती है — रूपांतरण, खोज, जोड़ना और हटाना, क्रमबद्ध करना, इटरेट करना और बनाना — ताकि आप एक नज़र में सही मेथड चुन सकें।
हर मेथड के लिए आप कॉल सिग्नेचर और यह संक्षिप्त नोट देखेंगे कि वह क्या लौटाता है और मूल array बदलता है या नहीं। JSON के साथ काम कर रहे हैं या आउटपुट फ़ॉर्मेट कर रहे हैं? <a href="/tools/javascript-beautifier">JavaScript Beautifier</a> आज़माएं।
Transforming
| Method | What it does |
|---|
map(fn) | Create a new array by transforming each element. |
filter(fn) | Create a new array with only the elements that pass a test. |
reduce(fn, init) | Reduce the array to a single accumulated value. |
reduceRight(fn, init) | Like reduce, but iterates right to left. |
flat(depth) | Flatten nested arrays up to the given depth. |
flatMap(fn) | Map each element, then flatten the result one level. |
Searching & Testing
| Method | What it does |
|---|
find(fn) | Return the first element that passes a test. |
findIndex(fn) | Return the index of the first matching element, or -1. |
findLast(fn) | Return the last element that passes a test. |
indexOf(value) | Return the first index of a value, or -1 if absent. |
includes(value) | Return true if the array contains a value. |
some(fn) | Return true if at least one element passes a test. |
every(fn) | Return true only if every element passes a test. |
Adding & Removing
| Method | What it does |
|---|
push(item) | Add an item to the end; returns the new length. |
pop() | Remove and return the last item. |
unshift(item) | Add an item to the start; returns the new length. |
shift() | Remove and return the first item. |
splice(start, count, ...items) | Add or remove items in place at any index. |
slice(start, end) | Copy a portion into a new array (non-mutating). |
concat(arr) | Merge one or more arrays into a new array. |
Ordering
| Method | What it does |
|---|
sort(fn) | Sort in place; pass a comparator for numeric order. |
reverse() | Reverse the order of elements in place. |
toSorted(fn) | Return a sorted copy without mutating the original. |
toReversed() | Return a reversed copy without mutating the original. |
Iterating
| Method | What it does |
|---|
forEach(fn) | Run a function once for each element (no return value). |
entries() | Return an iterator of [index, value] pairs. |
keys() | Return an iterator of the array's indices. |
values() | Return an iterator of the array's values. |
Creating & Joining
| Method | What it does |
|---|
Array.from(iterable) | Create an array from an iterable or array-like value. |
Array.of(1, 2, 3) | Create an array from the given arguments. |
Array.isArray(x) | Return true if a value is an array. |
join(separator) | Join all elements into a single string. |
fill(value) | Fill all elements with a static value in place. |
JavaScript Array Methods चीट शीट से जुड़े सामान्य प्रश्न
map और forEach में क्या अंतर है?
map हर एलिमेंट के लिए आपके लौटाए मान से बना एक नया array लौटाता है, इसलिए इसका उपयोग डेटा रूपांतरण के लिए होता है। forEach कुछ नहीं लौटाता — इसका उपयोग केवल लॉगिंग जैसे साइड इफेक्ट के लिए होता है। यदि आपको परिणाम चाहिए तो map उपयोग करें।
filter और find में क्या अंतर है?
filter टेस्ट पास करने वाले हर एलिमेंट का नया array लौटाता है। find केवल पहला मैचिंग एलिमेंट (या undefined) लौटाता है। सभी मैच चाहिए तो filter, केवल एक चाहिए तो find उपयोग करें।
JavaScript में reduce कैसे काम करता है?
reduce(fn, init) एक accumulator साथ लेकर array पर चलता है। हर चरण में यह fn(accumulator, currentValue) चलाता है और लौटाया मान अगला accumulator बन जाता है। इसका उपयोग array को एकल मान में समेटने के लिए होता है, जैसे योग या समूहित ऑब्जेक्ट।
कौन से array methods मूल array को म्यूटेट करते हैं?
push, pop, shift, unshift, splice, sort, reverse और fill array को उसी जगह बदलते हैं। map, filter, slice और concat जैसे मेथड नया array लौटाते हैं और मूल को अपरिवर्तित छोड़ देते हैं।
slice और splice में क्या अंतर है?
slice(start, end) array के एक हिस्से की प्रति बिना बदले लौटाता है। splice(start, count, ...items) एलिमेंट उसी जगह हटाता या जोड़ता है और मूल array को म्यूटेट करता है। नाम मिलते-जुलते हैं पर व्यवहार बहुत अलग है।
मैं संख्याओं के array को सही ढंग से कैसे सॉर्ट करूं?
एक comparator दें: बढ़ते क्रम के लिए arr.sort((a, b) => a - b)। comparator के बिना sort मानों को स्ट्रिंग में बदलता है, इसलिए [10, 2, 1] को [1, 10, 2] के रूप में सॉर्ट कर देगा। मूल को बदले बिना सॉर्ट करने के लिए toSorted उपयोग करें।
some और every में क्या अंतर है?
some(fn) तब true लौटाता है जब कम से कम एक एलिमेंट टेस्ट पास करे। every(fn) केवल तभी true लौटाता है जब सभी एलिमेंट पास करें। दोनों उत्तर ज्ञात होते ही जल्दी रुक जाते हैं, जो जांच के लिए इन्हें कुशल बनाता है।