I'm using Jquery.cookies plugin but I can't seem to find the information i'm looking for.
I have the cookie object which contains keys and values for cookies that are set on the page.
Want I need to do is loop through the object and check to see if a value exists inside the object and if it does log it to the console.
$.cookie().each(function() {
if (index == ratingsID) {
console.log(index)
}
});
With the above code I get an error that says undefined is not a function.
Can anyone point me in the right direction?
Thanks
if($.cookie("MyCookie") != null){
cookie = JSON.parse($.cookie("MyCookie"));
cookie.forEach(function(elem){
console.log(elem);
});
}
Try this
Related
function readProperty(property)
{
console.log(localStorage[property]) //Alerts “null”
if(localStorage[property] == null)
{
console.log('Null chek')
return false;
}
return localStorage[property];
}
log outputs "null", but 'if()' doesn't work. I try with ===, its not work too. Help please.
UPD: Thanks everyone this change helped me if(localStorage[property] == 'null')
The keys and the values stored with localStorage are always in the
UTF-16 string format, which uses two bytes per character. As with
objects, integer keys are automatically converted to strings.
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
Try:
localStorage[property] === 'null'
Although: console.log(localStorage[property]) may report null, the actual value is undefined.
So, in your if statement, if you test against undefined, you'll get a match.
Better yet though, just test for the existence or non existence of a value with:
if(localStorage[property])... // Tests for any "truthy" value
or
if(!localStorage[property])... // Tests for any "falsey" value
Well, I don't know if you're trying to use the global localStorage or if it's a defined variable in your code.
If you're using the localStorage API, you should check if a key exists like this...
if (!localStorage.getItem("my-item")) {
console.log("item doesn't exist.");
}
The .getItem() method returns null when the key isn't defined so checking using ! or item !== null work.
.getItem() reference from MDN, https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem.
you have to get item from localStorage using getItem() function like that
if(!localStorage.getItem(property) || localStorage.getItem(property)===null){
// there is no item in localStorage with the property name
}
So, I'm trying to check if the data from my .find() is empty. I can check if it's null`, but I realize that when the .find() finds nothing, it returns an empty list. When I have an if statement saying
if (docs = []) {
// code here
} else {
// code here
}
It doesn't do the code and instead does the code if the documents got from the database. How can I make it check if it's an empty list?
Check for length being 0 like if(docs.length === 0)
I have a series of if and else if statements and what I want is to determine a scenario in terms of local storage being empty.
I've tried:
(if localStorage.getItem('example') == localStorage.clear()) {
//Do something if that local storage item is empty
}
I am aware the program may think I'm assigning the local storage to clear out it's contents. So would I do something like:
(if localStorage.getItem('example') == localStorage()) {
//Do something if that local storage item is empty
}
If not, how can I refer to the local storage as an empty object so the program doesn't get confused thinking that I'd like to clear the contents in the storage?
You can check to see if it is null.
if(localStorage.getItem('example') === null){
console.log("x");
}
Ok both answers posted are right. However, I want to explain a few things that I have experienced. Undefined is returned if you are referring to the object as so and it has nothing in it:
localStorage.key; //will be undefined if not set
localStorage.getItem() returns null if nothing is in it (as I have experienced).
localStorage.getItem('key'); //will return null if not set
And yes, all you have to do is check if it is null or undefined (depending on the method you use)
if(localStorage.getItem('key') === null){
//do stuff here
}
or
if(localStorage.key === undefined){
//do stuff here
}
or you can use the ! operator like so:
if(!localStorage.getItem('key')){
//do stuff here
}
or
if(!localStorage.key){
//do stuff here
}
The reason for this is because both null and undefined are falsely values, and the ! operator checks for falsely values
You can just check if localStorage.getItem() is undefined. Here's an example:
function isCleared(item) {
return !localStorage.getItem(item)
}
I am receiving this JSON from server, and I need to check if it contains the key read.nores
if I do it like this
if (data[0]["read.nores"]) {
return;
}
it will crash because it does not contain that key.
How can I check if the key is there without a try/catch method, something like .has("read.nores")?
Reading an undefined key safely produces undefined, but reading a key from undefined will throw, so the problem will be specifically that data[0] doesn't exist, not that data[0]["read.nores"] doesn't exist.
To check for that, change it to:
if (data[0] && data[0]["read.nores"]) {
return;
}
It won't crash.
If you check any property of an object and it is not defined in the object it is by default undefined. So your if condition will work perfectly.
I have the following code snippet that defines the property values in my form.
function retrieve(){
setSelectedIndex(document.producerSetForm.GA1,"<%=ssnGA1%>");
setSelectedIndex(document.producerSetForm.GA2,"<%=ssnGA2%>");
setSelectedIndex(document.producerSetForm.GA3,"<%=ssnGA3%>");
setSelectedIndex(document.producerSetForm.GA4,"<%=ssnGA4%>");
setSelectedIndex(document.producerSetForm.GA5,"<%=ssnGA5%>");
}
where these ssnGA1,ssnGA2 etc may or may not be having a value. I need to check whether whether they have a value to do some more processing. I tried
var len=<%=ssnGA1.toString().length()%>;
if(len !=0)
but it works only if the value is present. else it giving javascript error. Please help. THANKS
You have to check if your string is not undefined/null first, on example:
if ( ssnGA1 && ssnGA1.toString().length ) {
// do something
}
Also, length is a property, not a function, see MDN for details.