0

I have a table stored as a string in a JSON data in localstorage. I want to compare one of fields stored here to a separate javascript variable.

Here is what I have tried:

var goalsStr = localStorage.getItem("goals");
var goalsObj = JSON.parse(goalsStr); 
for (i=0; i<goalsObj.goals.length; i++) {
  if (goal==goalsObj.goals[i].goal) {
    //.....
    //.....
  }
}

But it is not working. After some trouble shooting, I think that the problem is in comparing (goal == goalsObj.goals[i].goal).

And this is the value that was actually stored inside "goals" in localStorage:

var data = '{"goals": [{"goal":"'+goal+'","duedate":"'+date2+'","noofdays":"'+diff+'","active":"'+active+'"}]}';
localStorage.setItem("goals",data);

It is an array of objects stored within.

All these 'diff', 'duedate' are HTML form data taken from users.

What's wrong? What should I do?

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
  • 1
    We cannot really help you if we don't know the value of `goalsObj` or `goal`. You also have to explain what exactly you are trying to achieve and what the problem is. Do you *think* that the problem is the comparison or do you *know* it? FYI, the problem doesn't seem to have anything to do with JSON (unless the problem is generating proper JSON in the first place). – Felix Kling Dec 04 '13 at 05:38
  • It depends on what is `goal` and its type. Suppose they are object/array then they can not be compared with ==. – Sachin Jain Dec 04 '13 at 05:40
  • You will have to show us EXACTLY what was stored in localStorage. – jfriend00 Dec 04 '13 at 05:43
  • @FelixKling I have edited the question to answer your concerns. –  Dec 04 '13 at 05:44
  • @blunderboy Yes. They do involved Objects and Arrays. If not with == then what else should I use? –  Dec 04 '13 at 05:44
  • @jfriend00 I have edited the question and now you can see the data stored in localStorage. –  Dec 04 '13 at 05:45
  • Why are you calling JSON.stringify on a string? You should call JSON.stringify on a javascript object. What you're going to get back from `JSON.parse()` is a string, not a javascript object. – jfriend00 Dec 04 '13 at 05:45
  • @jfriend00 I have made that change, It's still not workin. –  Dec 04 '13 at 05:48
  • Works for me: http://jsfiddle.net/fLd5T/ – tom Dec 04 '13 at 06:01
  • @user221287 - simple debugging is where you start. Look at goalsObj in the debugger and see if it's exactly what you are expecting it to be. If not, you will know your next problem. If it is, then step through each comparison in your `for` loop and see why none are matching your goal variable. – jfriend00 Dec 04 '13 at 06:01
  • @tom Is it possible to store the localStorage in one file and acess it in a seperate file? –  Dec 04 '13 at 06:09
  • @tom http://stackoverflow.com/questions/20368160/can-localstorage-data-created-and-saved-in-one-html-file-can-be-used-in-another –  Dec 04 '13 at 06:12
  • @user221287 Yes, but looks like you already got an answer. – tom Dec 04 '13 at 06:19

1 Answers1

0

Data appears to already be valid JSON. Why are you stringifying it? I think you want to do just

var data = '{"goals": [{"goal":"'+goal+'","duedate":"'+date2+'","noofdays":"'+diff+'","active":"'+active+'"}]}';
localStorage.setItem("goals", data);

and then when you pull that value back out, convert it to a JavaScript object via

var obj = JSON.parse(localStorage.getItem("goals"));
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • Yes. You are absolutely right. But still I am not getting the output desired. There has to be some other error as well. –  Dec 04 '13 at 05:49
  • @user221287 - at this point you're just going to have to add some `debugger;` statements, run the code with the dev tools window open, and just step through it and see what's wrong. Your code looks ok, you likely made a silly mistake when splicing together that json string. – Adam Rackis Dec 04 '13 at 05:53
  • Is this the possible problem: http://stackoverflow.com/questions/20368160/can-localstorage-data-created-and-saved-in-one-html-file-can-be-used-in-another –  Dec 04 '13 at 06:10
  • Possibly - are you trying to access this piece of data on a different domain? – Adam Rackis Dec 04 '13 at 07:11
  • No. not on a different domain, but in a different .html file in my laptop. My whole project is offline. –  Dec 04 '13 at 07:13