-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.js
33 lines (27 loc) · 837 Bytes
/
iterator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// it is used to travel the array or object when we want next and next value each time
let fruits = [ "Apple", "Banana", "Grapes", "Orages", "Mango"]
function fruitsIterator(values){
let nextIndex = 0;
// we will return an object
return {
next: function (){
if(nextIndex < values.length ){
return {
value: values[nextIndex++],
done: false,
}
}else{
return {
done:true
}
}
}
}
}
console.log(fruits)
let newFunc = fruitsIterator(fruits) // this will create a new function each time.
console.log(newFunc.next())
console.log(newFunc.next().value)
console.log(newFunc.next())
console.log(newFunc.next())
console.log(newFunc.next())