1

In local storage i have this syntax

Key

#setupHospitalCity:hospitalCity  

Value

{"data":
[
    {"cityId":1,"city":"Abercorn"},
    {"cityId":2,"city":"ActonVale"},
    {"cityId":3,"city":"Adstock"},
    {"cityId":4,"city":"Aguanish"},
    {"cityId":5,"city":"Akulivik"}
],
"ttl":1443541460054}

With this code,

var values = localStorage.getItem("#setupHospitalCity:hospitalCity");   

i get all the values

how to loop only to these block

{"cityId":1,"city":"Abercorn"},
{"cityId":2,"city":"ActonVale"},
{"cityId":3,"city":"Adstock"},
{"cityId":4,"city":"Aguanish"},
{"cityId":5,"city":"Akulivik"}

3 Answers 3

1
var data = {"data":
            [
                {"cityId":1,"city":"Abercorn"},
                {"cityId":2,"city":"ActonVale"},
                {"cityId":3,"city":"Adstock"},
                {"cityId":4,"city":"Aguanish"},
                {"cityId":5,"city":"Akulivik"}
            ],
            "ttl": 1443541460054
        }

        for (var x = 0; x < data["data"].length; x++) {
            alert(data["data"][x].city)
        }
Sign up to request clarification or add additional context in comments.

1 Comment

data["data"] undefined
1
var data = values.data; // Returns a reference to the array.
for (var i = 0, ii = data.length; i < ii; ++i) { // Iterates over array.
    console.log(data[i]);
}

1 Comment

i get an undefined with values.data
1

You can loop it like any other array:

var values = localStorage.getItem("#setupHospitalCity:hospitalCity");   

var data = values.data;
for (var i = 0, len = values.data; i < len; i++) {
    console.log(values.data[i]); //values.data[i].cityId ...
}

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.