Reputation: 57
I have an array with some words there. I want to write a function which returns first value from array when function is called, but when function is called second time it returns second value, third time third value, when values ends in array, function returns undefined. Like:
let arr = ["text1", "text2", "text3"];
function getValue() {
...
}
getValue() \\ return text1;
getValue() \\ return text2;
getValue() \\ return text3;
getValue() \\ return undefined;
getValue() \\ return undefined;
Upvotes: 1
Views: 884
Reputation: 4015
Here's a a quick and dirty solution, which pollutes! the global scope (context):
let arr = ["text1", "text2", "text3"];
let idx = 0;
function getValue() {
return arr[idx++];
}
console.log(getValue());
console.log(getValue());
console.log(getValue());
console.log(getValue());
console.log(getValue());
Each execution context has a link to its outside world. So the execution context of getValue
has access to variable idx
(in this case global variable, because it is defined in the global scope). The outer environment (world) depends where the function is defined lexically (where it is written).
Better solution:
To not pollute the global scope you could switch to a higher order function (a function which returns a function)
function createGetValueFunction() {
const arr = ["text1", "text2", "text3"];
let idx = 0;
return function getValue() {
return arr[idx++];
};
}
const getValue = createGetValueFunction();
console.log(getValue());
console.log(getValue());
console.log(getValue());
console.log(getValue());
console.log(getValue());
Upvotes: 1
Reputation: 8558
If you're not using IE, you can use Generator Functions
to create such functionality, as followings:
let arr = ["text1", "text2", "text3"];
function* arrayIterator(index) {
while (index < arr.length) {
yield arr[index];
index++;
}
}
const getValue = arrayIterator(0)
console.log(getValue.next().value)
console.log(getValue.next().value)
console.log(getValue.next().value)
Upvotes: 0
Reputation: 23
I would say make sure that the function is keeping track of the position that you're looking at.
example:
const array = [1,2,3,4,5];
let pos = 0;
function test(arr) {
if(arr[pos]) {
pos++;
return(arr[pos - 1])
};
if(!arr[pos]) {
return(undefined);
};
};
console.log(test(array));
console.log(test(array));
console.log(test(array));
console.log(test(array));
console.log(test(array));
console.log(test(array));
Maybe something like this. Here I'm keeping track of the position, so I always know what position I need my function to be looking at. Then, I add 1 to the value so it knows to switch to the next element in the array. To be honest, I spent maybe about two seconds on this and there might be a much better way to do this. This is also something I feel would be easier to keep track of in a class of some sort.
Upvotes: 1
Reputation: 602
Use an varibale to manage the number of calls, and return undefined if number of calls is large than array length:
let arr = ["text1", "text2", "text3"];
let index = 0;
function getValue() {
if(index >= arr.length) return undefined;
return arr[index++];
}
console.log(getValue())
console.log(getValue())
console.log(getValue())
console.log(getValue())
console.log(getValue())
Upvotes: 1
Reputation: 2670
You can create a counter variable outside of the method as so:
let arr = ["text1", "text2", "text3"];
let ind = 0
function getValue() {
if(ind < arr.length){
console.log(arr[ind]);
}
else{
console.log("undefined")
}
ind+=1;
}
getValue()
getValue()
getValue()
getValue()
getValue()
Upvotes: 1
Reputation: 17400
Just hold a counter which is incremented on every call. Once it's after the end of the array, the function will return undefined
let arr = ["text1", "text2", "text3"];
let i = 0;
function getValue() {
return arr[i++];
}
getValue() // return text1;
getValue() // return text2;
getValue() // return text3;
getValue() // return undefined;
getValue() // return undefined;
Upvotes: 2
Reputation: 1347
You can use the splice
method.
The splice() method adds and/or removes array elements.
function getValue() {
const element = arr[0];
arr.splice(0,1); // At first position remove one element
return element;
}
Upvotes: 1