Good Evening, I am trying to increment an integer that I have index position of ‘0’ in my array, each time my function gets called. I have the variable added with .push, but then I just want to add one to that. I am trying to use indexof(), I have also tried findIndex(). Below is my code
const addFunction = async () => { var storage_array = await AsyncStorage.getItem(ASYNC_STORAGE_KEY); try { if(storage_array) { storage_array = JSON.parse(storage_array); let flow_complete = 0; var foundIndex = storage_array.indexOf(flow_complete); console.log(foundIndex); storage_array[foundIndex] = flow_complete++; await AsyncStorage.setItem(ASYNC_STORAGE_KEY, JSON.stringify(storage_array)); console.log('THIS IS THE ASYNCSTORAGE', storage_array); } else { flow_complete = 0; console.log('Storage array is empty') } } catch (error) { console.log(error); } }
Advertisement
Answer
Following the re-wording of the issue with your comment:
[…] the objective is to take the number ‘0’ that’s in the 0th position of the array and increment it by 1 each time the function runs
The first issue I see is that you may be misusing the indexOf
function. That will not give you the index of an array, but instead the position of a particular value of an array.
Example:
const arr = [9, 2, 7, 14] const index = arr.indexOf(9) // This will be 0, because the index of the number 9 in this array is 0 const otherIndex = arr.indexOf(7) // This will be 2, because the index of the number 7 in this array is 2
So, for you to access the element in the 0th position, you will want to do arr[0]
. So in your code you will want to do the following:
storage_array = JSON.parse(storage_array); let flow_complete = 0; // notice there is no need to get the `indexOf` 0 since you do want the position 0 storage_array[0] = flow_complete++;
Now… This will have a second problem which is your usage of the incrementation operator ++
. Although this will increment the flow_complete
variable, it does not return it to set storage_array[0]
as you intend to do so.
To fix this, all you have to do is increment flow_complete
prior to assigning it to storage_array[0]
. It will look something like this:
let flow_complete = 0; flow_complete++; storage_array[0] = flow_complete
However, if my interpretation of your comment above is correct, there is one more problem, which you are assigning flow_complete
to storage_array[0]
each time the function runs. flow_complete
is set to 0 as you can see in your own block of code within the scope of your addFunction
, so this means it will always go back to 0
every time it is run.
Going back to your original comment, you want to increment the value in the 0th index of storage_array
, not flow_complete
itself, correct?
If this is the case, you can completely get rid of the flow_complete
variable and instead increment storage_array[0]
. This will make your if-block look as follows:
if(storage_array) { storage_array = JSON.parse(storage_array); storage_array[0]++; await AsyncStorage.setItem(ASYNC_STORAGE_KEY, JSON.stringify(storage_array)); console.log('THIS IS THE ASYNCSTORAGE', storage_array); }