Search code examples
javascriptweblocal-storagesession-storage

Problems with extracting information from the Sessonstarage


I have problems with extracting the data from the sessionstorage and then putting the data in a vector to be able to traverse their positions, To see if the textarea were declared two variables with the same name, is for a project of a compiler

 var data = [];

function extraersesson() {


var medidor = JSON.parse(sessionStorage.length);

for (var i = 0; i < medidor; i++) {

    //var cap = information of sessionstorage
    var cap = JSON.parse(sessionStorage.getItem(i));


    data.push(cap);

    console.log(data[i] + " dataaa");

    if (data[i] == data[i + 1]) {

        console.log("the data is the same");

    }

   }

 }

But my problem arises here: image

Data is well saved in the sessionstorage

But when I want to extract and compare it gives me the problem

In the console. Log you can see that it carries the data but does not enter the If

image2

And do not run the other console. Log and just like I did as follows:

 function extraersesson() {


var medidor = JSON.parse(sessionStorage.length);

for (var i = 0; i < medidor; i++) {



    if (JSON.parse(sessionStorage.getItem(i)) == JSON.parse(sessionStorage.getItem(i+1))) {

        console.log("the data is the same");

    }


  }

 }

In summary I want to compare two values that are stored in the Sessionstorage


Solution

  • You can following code to get that output

    function extraersesson() {
      var data = [], medidor = JSON.parse(sessionStorage.length);
      for (var i = 0; i < medidor; i++) {
        var cap = JSON.parse(sessionStorage.getItem(i));
        if (data.indexOf(cap) >= 0) {
          console.log("The data is same");
        } else {
          data.push(cap);
        }
      }
    }