1

I am building ATM project and storing user data in local storage but after then I collect data and loop on this to match an existing user or creating a new user the data I can get is not be able to convert on JSON

after getting data from local storage I can't be able to convert to JSON for looping the data.

function User(id,pin,amount) {
    this.id = id,
    this.pin = pin,
    this.amount = amount
}
var memory = [];

function loginSignup(){
    var id= document.querySelector('.card').value;
    var pin= document.querySelector('.pass').value;
    var user = new User(id,pin);
    user = JSON.stringify(user);
    memory.push(user);
    localStorage.setItem('user', memory);
    var localData = [];
    localData.push(localStorage.getItem('user'));
    console.log(localData);
}



for(var i=0; i<localstorage.length; i++){
   if(localstorage[i].id == id){
          only allow update        }
        else{ update new user}

Like this is for understanding I want to loop in local storage data that users enter.

Ali Hassan
  • 33
  • 5
  • What's the output of `console.log(localData);`? If it looks like valid JSON, you should be able to convert it to an objecct using `JSON.parse()`. – Constantin Groß Oct 25 '19 at 11:14
  • Yes, but when I do JSON.parse() it gives the error. – Ali Hassan Oct 25 '19 at 11:19
  • Then please show us what error you receive and what the content of `localData` looks like - how are we supposed to help you otherwise? :) – Constantin Groß Oct 25 '19 at 11:19
  • the output of 'console.log(localData)' is in string format. – Ali Hassan Oct 25 '19 at 11:20
  • ["{"id":"1111111111111111","pin":"1234"},{"id":"2222…n":"1234"},{"id":"5555555555555555","pin":"1234"}"] 0: "{"id":"1111111111111111","pin":"1234"},{"id":"2222222222222222","pin":"1234"},{"id":"3333333333333333","pin":"1234"},{"id":"4444444444444444","pin":"1234"},{"id":"5555555555555555","pin":"1234"}"] I got these type of data from local storage, It can't be able to parse give an error. – Ali Hassan Oct 25 '19 at 11:23
  • Possible duplicate of [How do I store an array in localStorage?](https://stackoverflow.com/questions/3357553/how-do-i-store-an-array-in-localstorage) – Constantin Groß Oct 25 '19 at 11:25
  • this data ["{"id":"1111111111111111","pin":"1234"},{"id":"2222…n":"1234"},{"id":"5555555555555555","pin":"1234"}"] 0: "{"id":"1111111111111111","pin":"1234"},{"id":"2222222222222222","pin":"1234"},{"id":"3333333333333333","pin":"1234"},{"id":"4444444444444444","pin":"1234"},{"id":"5555555555555555","pin":"1234"}"] is wrong. – Prawin soni Oct 25 '19 at 11:33
  • "memory" is not stringified. – Prawin soni Oct 25 '19 at 11:36

1 Answers1

0

You need to use JSON.stringify while saving user data into storage:

localStorage.setItem('user', JSON.stringify(memory));

Now, whenever you need you can get the array back which will be parseable and traverseable:

var data = window.localStorage.getItem('user');
if (data) {
    data = JSON.parse(data);
    for (var i=0; i< data.length; i++) {
        if (JSON.parse(data[i]).id == id) { // see note below
           console.log("matched user", data[i])   
        }
        else {
            console.log("other user:", data[i])
        }
    }
}

Note; down the tree you also need to parse user-data array elements eg. JSON.parse(data[i]) because you have stringified that before pushing into memory array.

DEMO https://jsfiddle.net/pg2bsLve/1/

Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20