-
I have an alpine component that I setup like:
All of the @JSON(VAR) are PHP arrays. Then I have an alpine component called courseModule. In my component I am receiving the arrays like this:
and then setup my data:
Now to where my issue happens: I am trying to run a function that checks if there is a next index or if I am at the end. So I do the following: Then my function:
The issue is that it returns false even though I know there is a next item in the array. If I console the array I get: Proxy(Array) {0: 1, 1: 2} How can I get the next item in this sort of setup, or how can I refactor to handle this better? thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You just need to do To meet it seems you could just rewrite this as return arr[arr.indexOf(item)+1] For the same effect. Is it possible to pass in an item that ISNT present? If not, then you never need to check for -1. If it's just that it might be return item && arr[arr.indexOf(item)+1] And if the index if out of bounds, it returns If you are looping over these in Alpine, you can get the index in the for loop and directly use that instead of doing this extra loop to get the index. Or even use a stored iterator instead. |
Beta Was this translation helpful? Give feedback.
You just need to do
index < arr.length
you added a-1
for no reason.To meet it seems you could just rewrite this as
For the same effect. Is it possible to pass in an item that ISNT present? If not, then you never need to check for -1.
If it's just that it might be
undefined
then you can just doAnd if the index if out of bounds, it returns
undefined
, which is already what your function does if the index is too high.If you are looping over these in Alpine, you can get the index in the for loop and directly use that instead of doing this extra loop to get the index.
Or even use a stored iterator instead.