I have a requirement where I need to access an array element using its function.
For example, I have Array A[], now I want to create array B, such that
A[i] === B[i].value()
I tried below code but I am getting error as B[i].value is not a function
<script>
function test(A) {
var B = new Array();
for(var i=0; i<A.length; i++) {
B[i] = function value() {
return A[i];
};
}
for(var i=0; i< B.length; i++) {
console.log(B[i].value());
}
return B;
}
A=[1,2,3];
B = test(A);
</script>
What is the correct way for this?
A[i] === B[i].value()- there is smth wrong