JS ऐरे मेथड्स
In this page:
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 करते हैं।
उदाहरण: Adding and Removing: push, pop, shift, unshift
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 स्ट्रिंग में बदल देता है।
उदाहरण: splice, slice, concat, and join
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।
उदाहरण: indexOf and find
// 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 ऐरे को नहीं छेड़ते।
उदाहरण: filter and map
// 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 न दिया जाए।
उदाहरण: reduce, forEach, and sort
// 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));
- map(), जो एक नई transformed array return करता है, को forEach(), जो कुछ return नहीं करता और सिर्फ side effects के लिए इस्तेमाल होता है, से confuse करना।
- यह भूल जाना कि filter() और map() नई arrays return करते हैं, original array को पूरी तरह unchanged छोड़ते हुए।
- 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: