Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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!

share|improve this question
    
Use var somevar = JSON.stringify(report); and then print somevar – akhil gupta Jan 22 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? – Jarlik Stepsto Jan 22 at 22:15
    
You are correct. – user3259138 Jan 22 at 22:17
up vote 3 down vote accepted

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

share|improve this answer
    
Testing this... :) – user3259138 Jan 22 at 22:18
    
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 – chriskelly Jan 22 at 22:19
    
Odd... Instead of returning undefined, it returned blank...? – user3259138 Jan 22 at 22:20
    
returns blank if ((rs != null) && (rs != '')) is not true. Are you sure localstorage contains something? probably want to uncomment that console.log – chriskelly Jan 22 at 22:20
    
haha I had deleted the localstorage. I will also add and else clause to create localstorage if it doesn't exist. Thank you! – user3259138 Jan 22 at 22:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.