0

I have an array of objects saved in localstorage.

Here is a sample of the array of objects:

[{"mls":"1266159","addr":"13556 S TUSCALEE ,  Draper, UT 84020","price":"$1499000","description":"Bds:  7  | Ba:  7  |  10376 Sqft."},{"mls":"1279829","addr":"12927 S SALZ ,  Draper, UT 84020","price":"$779000","description":"Bds:  4  | Ba:  4  |  5857 Sqft."},{"mls":"1279960","addr":"14226 S CANYON VINE E,  Draper, UT 84020","price":"$1899000","description":"Bds:  7  | Ba:  8  |  10015 Sqft."}]

I have written a function to get the array, then run an "each" loop to iterate over the objects and return all objects in the array, in the specified format.

Function:

function getReportStore() {
        var rs = localStorage.getItem("reportStore");
        //console.log("Report Store: "+rs);
        if((rs != null) && (rs != '')){
            var r = JSON.parse(rs);
            $.each(r, function (k, v) {
                return "MLS: "+v.mls+" Address: "+v.addr+" Price: "+v.price+"    Description: "+v.description;
            });
        }
    }

Trying to call function, and return the data into a variable...

var p = getReportStore();

Now use the variable...

var report = "Properties Viewed: " + p;
console.log("The Report: "+report);

The problem, is that when I try and execute the function in order to use the returned objects as a variable, it comes back as "undefined".

Thank you in advance for your suggestions/assistance/guidance/etc!

2
  • Use var somevar = JSON.stringify(report); and then print somevar Commented Jan 22, 2016 at 22:14
  • 2
    where exactly is it undefined? .... what is the output of getReportStore? this function does not return anything, or am i wrong? Commented Jan 22, 2016 at 22:15

1 Answer 1

3

Looks like you are not returning anything from your function. You are returning from the inner higher order $.each function but this does not return from getReportStore().

Try it like this :

function getReportStore() {
    var rs = localStorage.getItem("reportStore");
    //console.log("Report Store: "+rs);
    if((rs != null) && (rs != '')){
        var r = JSON.parse(rs);
        return r.map(function (v) {
            return "MLS: " +v.mls +
                " Address: "+v.addr +
                " Price: "+v.price +
                "    Description: " + v.description;

        }).join('\n');
    }
    return "";
}

note: I replaced jquery $.each with standard array map

Sign up to request clarification or add additional context in comments.

4 Comments

good luck. btw. I added a join because map (and $.each) will return an array. I presume you want to join them together. Otherwise you could leave it out and get an array back
Odd... Instead of returning undefined, it returned blank...?
returns blank if ((rs != null) && (rs != '')) is not true. Are you sure localstorage contains something? probably want to uncomment that console.log
haha I had deleted the localstorage. I will also add and else clause to create localstorage if it doesn't exist. Thank you!

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.