JS इटरेबल्स
In this page:
for (const item of iterable) {
// item is each element
}
const iterator = iterable[Symbol.iterator]();
कोई चीज़ Iterable कैसे बनती है
कोई value तब iterable मानी जाती है जब वह एक खास internal method लागू करती है, जिसे Symbol.iterator के ज़रिए access किया जा सकता है, और जो यह तय करता है कि उसकी values में एक-एक करके कैसे आगे बढ़ा जाए।
उदाहरण: What Makes Something Iterable
const arr = [1, 2, 3];
console.log(typeof arr[Symbol.iterator]); // "function", arr is iterable
Strings के साथ for...of
Strings iterable होती हैं, यानी for...of किसी स्ट्रिंग में एक-एक character करके आगे बढ़ सकता है, और कुछ पुराने indexing तरीकों से बेहतर तरीके से multi-character symbols को सही ढंग से संभालता है।
उदाहरण: for...of With Strings
// Declare the constant `word`, set to "Hi"
// Declare the constant `word`, set to "Hi"
const word = "Hi";
// Loop over `word`, binding each item to `char`
// Loop over `word`, binding each item to `char`
for (let char of word) {
// Print `char` to the console
// Print `char` to the console
console.log(char);
}
Arrays के साथ for...of
Arrays for...of के साथ इस्तेमाल होने वाला सबसे आम iterable है, जो बिना किसी manual index tracking के हर element की value तक सीधी पहुंच देता है।
उदाहरण: for...of With Arrays
// Declare the constant `nums`, set to `[10, 20, 30]`
// Declare the constant `nums`, set to `[10, 20, 30]`
const nums = [10, 20, 30];
// Loop over `nums`, binding each item to `n`
// Loop over `nums`, binding each item to `n`
for (let n of nums) {
// Print `n` to the console
// Print `n` to the console
console.log(n);
}
Maps और Sets के साथ for...of
Map और Set दोनों, जिन्हें बाद के chapters में विस्तार से कवर किया गया है, built-in iterables हैं, जो for...of को किसी Set की values या किसी Map के key-value pairs में स्वाभाविक रूप से आगे बढ़ने देते हैं।
उदाहरण: for...of With Maps and Sets
// Declare the constant `set` as a new `Set` instance
// Declare the constant `set` as a new `Set` instance
const set = new Set([1, 2]);
for (let v of set) console.log(v);
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map([["a", 1]]);
for (let [k, v] of map) console.log(k, v);
Iterables क्यों मायने रखते हैं
यह shared iteration protocol ही है जो एक single, consistent for...of syntax को कई बिल्कुल अलग-अलग data structures पर काम करने देता है, जो JavaScript को collections process करने का एक unified तरीका देता है, बिना हर type के लिए अलग looping style सीखे।
उदाहरण: Why Iterables Matter
// Define the function `sum` taking `iterable`
// Define the function `sum` taking `iterable`
function sum(iterable) {
// Declare the variable `total`, set to `0`
// Declare the variable `total`, set to `0`
let total = 0;
for (let n of iterable) total += n;
// Return `total` from this function
// Return `total` from this function
return total;
}
// Print `sum([1, 2, 3])` to the console
// Print `sum([1, 2, 3])` to the console
console.log(sum([1, 2, 3]));
// Print `sum(new Set([4, 5]))` to the console
// Print `sum(new Set([4, 5]))` to the console
console.log(sum(new Set([4, 5])));
- यह मान लेना कि हर object automatically iterable है, plain objects डिफ़ॉल्ट रूप से iterable नहीं होते जब तक आप खासतौर पर वह behavior implement न करें।
- किसी plain object पर सीधे for...of इस्तेमाल करने की कोशिश करना, जो एक TypeError throw करता है क्योंकि objects के पास default iteration protocol नहीं होता।
- array-like objects, जैसे function arguments, को असली iterables से confuse करना, कुछ array-like structures को पहले conversion चाहिए।
- एक iterable कोई भी ऐसी value है जो iteration protocol implement करती है, जिससे इसे for...of से loop किया जा सके।
- Symbol.iterator वह special method है जो किसी value को अंदर से iterable बनाता है।
- Strings, arrays, Sets, और Maps सभी built-in iterables हैं, जबकि plain objects डिफ़ॉल्ट रूप से iterable नहीं हैं।
Iteration protocol, Symbol.iterator, और built-in iterables के लिए for...of support हर modern browser में supported हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic