← Back to JavaScript Course | Chapter 3: Strings, Arrays & Data | Lesson 4 of 6

JS ऐरे मेथड्स

एक warehouse team की कल्पना करें जिसमें specialized workers हों, एक जो shelf के अंत में नए boxes जोड़ता है, एक जो कोई खास box ढूंढता है, एक जो हर box चेक करके एक summary बताता है, हर किसी का एक साफ काम। JavaScript ऐरे methods आपके data के लिए वही specialized team हैं, push एक item जोड़ता है, find एक item ढूंढता है, filter किसी list को छाँटकर छोटा करता है, और map हर item को एक साथ transform करता है, यह सब बिना आम कामों के लिए manual loops लिखे। असली applications, जिनमें cookiescursor.com के features भी शामिल हैं, tutorials, scores, या user data की lists को कुशलता से और readable तरीके से process करने के लिए इन्हीं methods पर बहुत निर्भर रहते हैं।
Syntax
javascript
array.push(item);
array.pop();
array.map(item => expression);
array.filter(item => condition);
array.slice(start, end);

जोड़ना और हटाना: push, pop, shift, unshift

push() ऐरे के अंत में एक element जोड़ता है, pop() आखिरी element हटा देता है, unshift() शुरुआत में एक element जोड़ता है, और shift() पहला element हटा देता है, यह सभी original ऐरे को सीधे modify करते हैं।

Note: push() और pop() आमतौर पर unshift() और shift() से तेज़ होते हैं, क्योंकि अंत से जोड़ने या हटाने में बाकी सभी elements को reindex नहीं करना पड़ता।
Warning: pop() और shift() दोनों ही हटाए गए element को return करते हैं, modified ऐरे को नहीं, जो हैरान कर सकता है अगर आप ऐरे खुद वापस आने की उम्मीद कर रहे थे।

उदाहरण: Adding and Removing: push, pop, shift, unshift

javascript
const list = [2, 3];
list.push(4);
list.unshift(1);
console.log(list); // [1, 2, 3, 4]
list.pop();
list.shift();
console.log(list); // [2, 3]

splice, slice, concat, और join

splice() किसी भी position पर elements जोड़ता या हटाता है और ऐरे को in place modify करता है, slice() original को बदले बिना एक हिस्सा निकालता है, concat() ऐरे को मिलाकर एक नई ऐरे बनाता है, और join() ऐरे को एक single स्ट्रिंग में बदल देता है।

Note: जब आपको original को छुए बिना एक copy या हिस्सा चाहिए तो slice() इस्तेमाल करें, और जब आप खुद ऐरे को modify करना चाहें तो खासतौर पर splice()।
Warning: splice() के arguments याद रखना आसान नहीं है, दूसरा argument यह बताता है कि कितने elements हटाने हैं, slice() जैसे किसी end index की तरह नहीं।

उदाहरण: splice, slice, concat, and join

javascript
const nums = [1, 2, 3, 4];
console.log(nums.slice(1, 3));   // [2, 3], original untouched
nums.splice(1, 1, "x");
console.log(nums);               // [1, "x", 3, 4]
console.log([1, 2].concat([3, 4]));
console.log(nums.join("-"));

indexOf और find

indexOf() पहले matching element की position return करता है, या न मिलने पर -1, जबकि find() दिए गए test function को पूरा करने वाला पहला element return करता है, या कोई मैच न होने पर undefined।

Note: साधारण value matches के लिए indexOf() इस्तेमाल करें, और जब आपकी search condition एक साधारण equality check से ज़्यादा जटिल हो तो find()।
Warning: indexOf() सिर्फ exact matches जांचता है, यह किसी object को उसकी किसी property से नहीं ढूंढ सकता, ऐसी search के लिए find() ज़रूरी है।

उदाहरण: indexOf and find

javascript
// Declare the constant `nums`, set to `[10, 20, 30]`
// Declare the constant `nums`, set to `[10, 20, 30]`
const nums = [10, 20, 30];
// Print `nums.indexOf(20)` to the console
// Print `nums.indexOf(20)` to the console
console.log(nums.indexOf(20));
// Print `nums.find(n => n > 15)` to the console
// Print `nums.find(n => n > 15)` to the console
console.log(nums.find(n => n > 15));

filter और map

filter() एक नई ऐरे बनाता है जिसमें सिर्फ वही elements होते हैं जो दिए गए test को पास करते हैं, जबकि map() एक नई ऐरे बनाता है जिसमें हर element किसी दिए गए function से transform हो चुका होता है, दोनों ही original ऐरे को नहीं छेड़ते।

Note: filter() और map() अक्सर एक साथ chain किए जाते हैं, पहले किसी list को filter करके छोटा किया जाता है और फिर जो बचता है उसे transform किया जाता है, सब एक readable expression में।
Warning: filter() और map() दोनों हमेशा एक नई ऐरे return करते हैं, भले ही वह खाली हो, यह कभी भी original ऐरे को in place modify नहीं करते।

उदाहरण: filter and map

javascript
// Declare the constant `nums`, set to `[1, 2, 3, 4, 5]`
// Declare the constant `nums`, set to `[1, 2, 3, 4, 5]`
const nums = [1, 2, 3, 4, 5];
// Print `nums.filter(n => n % 2 === 0)` to the console
// Print `nums.filter(n => n % 2 === 0)` to the console
console.log(nums.filter(n => n % 2 === 0));
// Print `nums.map(n => n * 2)` to the console
// Print `nums.map(n => n * 2)` to the console
console.log(nums.map(n => n * 2));

reduce, forEach, और sort

reduce() ऐरे के हर element को मिलाकर एक single accumulated value बनाता है, forEach() सिर्फ side effects के लिए हर element पर एक बार function चलाता है और कुछ उपयोगी return नहीं करता, और sort() ऐरे के elements को फिर से क्रम में लगाता है, डिफ़ॉल्ट रूप से उन्हें स्ट्रिंग्स की तरह मानते हुए जब तक कोई custom compare function न दिया जाए।

Note: Numeric sorting के लिए sort() को हमेशा एक compare function दें, जैसे (a, b) => a - b, वरना numbers टेक्स्ट की तरह गलत क्रम में sort हो जाते हैं।
Warning: sort(), filter और map के उलट, original ऐरे को in place modify करता है, यह भूलना आसान है जब आप सिर्फ एक sorted order preview करना चाहते थे।

उदाहरण: reduce, forEach, and sort

javascript
// Declare the constant `nums`, set to `[4, 2, 7]`
// Declare the constant `nums`, set to `[4, 2, 7]`
const nums = [4, 2, 7];
// Print `nums.reduce((sum, n) => sum + n, 0)` to the console
// Print `nums.reduce((sum, n) => sum + n, 0)` to the console
console.log(nums.reduce((sum, n) => sum + n, 0));
// Call `nums.forEach(n => console.log(n))`
// Call `nums.forEach(n => console.log(n))`
nums.forEach(n => console.log(n));
// Print `[...nums].sort((a, b) => a - b)` to the console
// Print `[...nums].sort((a, b) => a - b)` to the console
console.log([...nums].sort((a, b) => a - b));
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. map(), जो एक नई transformed array return करता है, को forEach(), जो कुछ return नहीं करता और सिर्फ side effects के लिए इस्तेमाल होता है, से confuse करना।
  2. यह भूल जाना कि filter() और map() नई arrays return करते हैं, original array को पूरी तरह unchanged छोड़ते हुए।
  3. push() इस्तेमाल करना और वापस एक नई array की उम्मीद करना, push() array को in place modify करता है और नई length return करता है, array खुद नहीं।
चैप्टर सारांश
  • push, pop, shift, और unshift किसी array की शुरुआत या अंत से elements जोड़ते या हटाते हैं।
  • filter, find, और map हर element पर लागू किसी condition या transformation के आधार पर नई arrays या values बनाते हैं।
  • reduce हर element को एक single value में combine करता है, और forEach कुछ भी return किए बिना हर element के लिए एक बार function चलाता है।
ब्राउज़र सपोर्ट

यहाँ बताए गए सभी standard array methods हर modern browser में supported हैं और JavaScript का एक stable, foundational हिस्सा माने जाते हैं।

🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.