8

I am facing a problem in javascript array...i am using Serversent event in JS. I will be getting few values from server frequently...

My task is to catch all the details and display in drop-down box...

Now the problem is, during the first request, i will getting values seperated by comma.. I have array object in js...i will check if the array is empty, if so, then i will include the values in combo...

code:

var varArr = new Array();


//since i am using SSE, i will executing this below part multiple times automaticall when ever server pushes data..

if(!varArr.length){
varArr[0]='somevalue';
//Printing some value in <div>
}
else{
//some task...to print in <div>
}

Since i have added some values in array if the array is empty, i am not getting any values printed in div (//Printing some value in ), instead i am getting (//some task...to print in )

7
  • 3
    I don't know if it matters because you didn't provide much code, but arrays start at 0 not 1 Commented Sep 12, 2013 at 16:04
  • i have updated the question at the end...Please help on this Commented Sep 12, 2013 at 16:08
  • using jquery to print some value in div. my condition if(!varArr.length) -> returns true if array is empty..and as I enter the condition, i add value in array...But problem is if I dont add any element in array inside the condition, it prints the value in div...Now my code automatically moves to else part Commented Sep 12, 2013 at 16:14
  • computers start a zero, not one, but that's probably not the issue... Commented Sep 12, 2013 at 16:14
  • updated the post...corrected the index...but problem persists... Commented Sep 12, 2013 at 16:21

1 Answer 1

6

Perhaps you mean to do this?

var varArr = new Array();
if (varArr.length === 0) {
  varArr.push(somevalue);
  // Printing some value in <div>
} else {
  // Some task...to print in <div>
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, mistake in my post varArr[0]='somevalue';..but the problem persists in all case...

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.