Currently, I'm attempting to achieve the following task. I have an array structured as such:
var my_array = ['1', '2', '3' ... ,'1000000000000000'];
My objective is to generate multiple HTML elements for each item in the array. Given that the array can potentially contain a large number of elements, I tried implementing the following approach to prevent the browser from freezing.
for(var i in my_array)
{
if(my_array.hasOwnProperty(i))
{
setTimeout(function(){
do_something_with_data(my_array[i]);
});
}
}
However, I encountered an issue where the value of my_array[i] within the setTimeout function does not match what it should be.
To provide more detail, when I attempt to console.log(my_array[i])
, the output appears as follows:
"getUnique" function (){
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
The "getUnique" function happens to be one that I added to the Array prototype in the following manner:
Array.prototype.getUnique = function(){
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
};
I would greatly appreciate any assistance in resolving this issue. Can someone please help me with this?