0

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

3
  • 1
    An empty slot is literally that - it's empty. It's doesn't have any value assigned to it, not the string "empty", not null which is a value, nor even undefined which is also a value. An array that contains empty slots is called a sparse array. Using .forEach or any of the other array iteration methods will not go over the empty slots, only filled ones. Commented Apr 3, 2020 at 11:47
  • 1
    You should simply check if the slot you're trying to add to is filled and if not, then fill it: 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. Commented Apr 3, 2020 at 11:49
  • Yes, that was what I need, something like that, I didnt think of starting by the negation, also knowing the index yes I can create an array with that lenght, I prefer first option thank you very much!! Commented Apr 3, 2020 at 11:52

1 Answer 1

1

hope its will work

//here is new Array N terms

    var newArray = Array.apply(null,Array(25)).map(a=>null) 

    //newArray[index] = append value

    newArray[2] = 'Hello_1'
    newArray[5] = 'Hello_2'
    newArray[7] = 'Hello_3'
    newArray[11] = 'Hello_4'

    console.log('Append',newArray,'or other show null')

Sign up to request clarification or add additional context in comments.

3 Comments

I'm gonna check in a few hours, looks really nice, thank you (:. I'll let you know later!
Sorry for the delay @walled-naser . Your answer fits well with what I needed, so thank you so much, could you please provide some text to explain what did you do?? :) . I have marked as the right one.
Hi also sorry for the delay, here is my answer, i created new Array which length is 25 and value is empty so then i place map ".map(a=>null)" and replace empty to null, thats allow to assign any value to specific index,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.