0
votes

I wanna use show / hide div function with localStorage. So I wrote this code scope:

 function getLocalDatas (clickedBefore, data, nid, vote, advpoll_choice, title, element) {
        for(var nid in actualPolls){

          var nids = [];
          var nids = JSON.stringify(actualPolls[nid].nid);
          // console.log('id '+ ids);
          console.log('here '+ nids);
        }
        for(var i in localStorage){

          var ids = JSON.stringify(localStorage[i]);
          // console.log('id '+ ids);
          console.log('nid '+ ids);
        }
        if(ids == nids) {
          console.log('ids '+ids);
          vm.clickedBefore = true;
          console.log(vm.clickedBefore);
          console.log("done");

        } else {
          console.log('ids '+ids);
          console.log('nids '+nids);
          console.log("not done");
        }

     // return i;
   }
   getLocalDatas();

I checked for values on console.log I dont know why but it shows last items each values. I need sort for values and use them in condition. Whats the wrong? How can I solve it. Thanks.

2
Please clarify your question: what is the expected results vs actual? Thanks and regards, - Alexander Bell
My primary problem is for items is not array, its getting just last result. - Censin DO
What is the expected result? Btw, you are comparing two arrays (not array elements) - is it the correct business logic? - Alexander Bell
Well, I think I have to compare array elements. How can I do it? - Censin DO
Console is showing the last value because you are replacing it with the value. Every time the for loop runs it will replace with the new value - Anil Shrestha

2 Answers

0
votes

Answering your question based on additional input provided in comments: in order to compare array elements use the index, like nids[1], nids[2] or nids[i] if within the for loop. The same applies to the second array ids. Hope this will help.

0
votes
    function getLocalDatas (clickedBefore, data, nid, vote, advpoll_choice, title, element) {
    for(var nid in actualPolls){

      var nids = [];
      var nids[]= JSON.stringify(actualPolls[nid].nid);
      // console.log('id '+ ids);
      console.log('here '+ nids);
    }
    for(var i in localStorage){
      var ids = [];
      var ids[] = JSON.stringify(localStorage[i]);
      // console.log('id '+ ids);
      console.log('nid '+ ids);
    }
    for(var i = 0; i< ids.length; i++){
    if(ids[i] == nids[i]) {
      console.log('ids '+ids);
      vm.clickedBefore = true; 
      console.log(vm.clickedBefore);
      console.log("done");

    } else {
      console.log('ids '+ids);
      console.log('nids '+nids);
      console.log("not done");
    }
    }

 // return i;
    }
    getLocalDatas();

I think this is what you want code is not ordered . It's hard to order from mobile