1

Is it possible to compare an object property to a variable stored in localstorage using Javascript or Jquery? Here's what I mean...

So I have an object like this:

var persons = [
  {
    "firstName": "Dwight",
    "surName": "Stender",
    "picture": "https://randomuser.me/api/portraits/med/men/99.jpg",
    "id": "1"
  }
]

And I have a variable stored in localstorage (the name is clickId). Now I want to compare the id from the object to that variable in localstorge. I can print them both to the console so I know they work. But when I try to compare them, it suddenly doesn't work anymore (even though the id and the variable contain the same number). This is how I compared the two:

for (i = 0; i < persons.length; i++) {
    if (persons[i].id == localStorage.clickId) {
        document.getElementById("result").innerHTML = "yay"
    } else {
        document.getElementById("result").innerHTML = "nay"
    };
};

Note 1 I have also tried comparing the two with === instead of ==

Note 2 the statements to be executed inside the if...else are just placeholders for the purpose of explanation

1
  • What is the problem in this code? Commented Aug 7, 2017 at 13:22

2 Answers 2

5

You need to use localStorage.getItem(key) to retrieve the object you previously stored. Also note that as localStorage can only hold string you'd need to serialise the object before/after saving it. JSON would be ideal for this:

Also note that you can use some() to negate the for loop, and a ternary to simplify the if.

var persons = [{
  "firstName": "Dwight",
  "surName": "Stender",
  "picture": "https://randomuser.me/api/portraits/med/men/99.jpg",
  "id": "1"
}];

var person = JSON.parse(localStorage.getItem('person') || '{}');
document.getElementById("result").innerHTML = persons.some(p => p.id == person.clickId) ? "yay" : 'nay';

localStorage.setItem('person', JSON.stringify({
  clickId: 1
}));
<div id="result"></div>

Working example

Sign up to request clarification or add additional context in comments.

6 Comments

So you can't compare them directly? You have to store the localstorage variable inside another variable?
Yes - the localStorage is a dictionary containing key/value pairs. It does not store information in the manner you're expecting. I updated the answer to give you a more complete example too.
Okay I see, just to be clear: if I have "(clickId, 1)" already saved in localstorage, it would be enough for me to change the getItem('person') to getItem('clickId')?
How do I replace the "yay" (if it's true) with the property firstName from the persons object? If I print anything other than plain text, it doesn't show... Is that because of the some() method or because of the shortened if...else statement?
Does it need to be the firstName of the found person? If so that's a fundamental logic change
|
0

you can use the inArray method of Jquery without need to use the loop

$.inArray(value, array)

Returns index of value in array. Returns - 1 if the array does not contain the value

Comments

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.