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 )
Answer: Use the array_push() Function You can simply use the array_push() function to add new elements or values to an empty PHP array.
We can also explicitly check if the array is empty or not. if (arr. length === 0) { console. log("Array is empty!") }
JavaScript Array push() The push() method adds new items to the end of an array. The push() method changes the length of the array.
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>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With