0

I have gotten the answer I needed for my initial question but now I have another issue with the same array, I am using the getItem and setItem method to only display the iziToast popup once and after it has displayed once not display again (as I have the following array in a function that is within a setInterval function that runs every 30 seconds so essentially every 30 seconds the iziToast popup keeps popping up which I don't want it to do).

Here is the array:

var alerted = localStorage.getItem('alerted') || '';

for(var i in result) {

                console.log(result[i].id);

                        if (alerted != result[i].id) {
                            $('#chatAudio')[0].play();

                            iziToast.show({
                                message: result[i].notification_text,
                                messageColor: '#424242',
                                backgroundColor: '#fff',
                                theme: 'light',
                                animateInside: true,
                                layout: 1,
                                close: false,
                                position: 'bottomLeft',
                                timeout: 5000,
                                progressBar: false
                            });

                            localStorage.setItem('alerted', result[i].id);
                        }
                }
6
  • 2
    where is your array ? Commented Jul 13, 2018 at 19:52
  • 4
    SO use a for loop and start at the 5th index? Commented Jul 13, 2018 at 19:52
  • I have edited my question, please take a look at the new question. Thanks Commented Jul 13, 2018 at 21:39
  • You should ask another question and post your full code (including how you are calling setIntervals, etc...). Commented Jul 13, 2018 at 21:45
  • @FelipeLanza ok no problem, will do in a minute Commented Jul 13, 2018 at 22:00

4 Answers 4

2

Can't you loop only through the desired range?

var startIndex = 4 // 5th element

for(var i=startIndex; i < results.length; i++) {
    // (...)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have edited my question, please take a look at the new question. Thanks
1

Someone posted an answer a few minutes ago and it was correct and it worked but for some reason it was deleted so I'm reposting it for anyone who may have missed it.

Here it is:

for(var i in result) {
                if (i < 5) continue;

                console.log(result[i].id);

                        if (alerted != result[i].id) {
                            $('#chatAudio')[0].play();

                            iziToast.show({
                                message: result[i].notification_text,
                                messageColor: '#424242',
                                backgroundColor: '#fff',
                                theme: 'light',
                                animateInside: true,
                                layout: 1,
                                close: false,
                                position: 'bottomLeft',
                                timeout: 5000,
                                progressBar: false
                            });

                            localStorage.setItem('alerted', result[i].id);
                        }
                }

Comments

1

MDN(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) says:

Note: for...in should not be used to iterate over an Array where the index order is important.

It seems like in your case order IS important.

Still you could check in which iteration you are by:

result.indexOf(i)

so you can continue while your iteration index is < 4 like this:

if(result.indexOf(i) < 4) continue;

1 Comment

I have edited my question, please take a look at the new question. Thanks
0
for (let i = 4; i < result.length; i++) {
  console.log(result[i]);
}

1 Comment

I have edited my question, please take a look at the new question. Thanks

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.