Good morning, I am trying to append a value to an empty array declared in a global scope. This array will holds the values of enums results, placed in the same position given by an index, something like this:
array[index] += result
So when the array is empty and I assign that way give me this in chrome console:
(2) [empty, NaN]
Then if I store that result as global variable and copy paste here it gives us this:
[
null,
null
]
What I was trying before was something like this:
if (array[index] == undefined) {
//first try
array[index] += result;
//if 'empty'(better null?) put 0 for now
array.forEach(x => x == "empty" ? x = 0 : x = result)
//second try
array.splice(index, index(plus 1?), result);
}
Because what I want is something like this:
"With a empt array append data in n-position leaving (positions < n-position) with value = 0"
What do you think about this?
Thank you very much,
qiqke
"empty", notnullwhich is a value, nor evenundefinedwhich is also a value. An array that contains empty slots is called a sparse array. Using.forEachor any of the other array iteration methods will not go over the empty slots, only filled ones.if (!array[index]) { array[index] = 0; } array[index] += result. If you know what indexes you need, you could just generate an array filled with zeroes to begin with.