0

This function seems to be rewriting the value for the 'pages' variable and I'm at my wit's end on this one.

I've tried returning the variable through a function, hardcoding the variable into the function, and a pile of other things, but this seems like it should work.

Any ideas?

EDIT:

The output from should be an array of objects formatted like this {default: "Tax Return", safe: "taxreturn"}. The function, when first called with getPages('Both', 'overview', null) and getPages('Both', null, 'overview') does this, but if you call it more times it will error and you will find that the 'pages' variable is now an array of objects.

var pages = [
    "Dashboard",
    "Overview",
    "Contacts",
    "Records",
    "Cash Flow",
    "Transactions",
    "Income",
    "Expenses",
    "Tax Return"
];

var getPages = function(format, includeOne, excludeOne)
{
    var pageStrings = pages;

    if(includeOne)
        for(var p = 0; p < pageStrings.length; p++)
            if(uriSafe(pageStrings[p]) == uriSafe(includeOne))
                pageStrings = [pageStrings[p]];

    if(excludeOne)
        for(var c = 0; c < pageStrings.length; c++)
            if(uriSafe(pageStrings[c]) == uriSafe(excludeOne))
                pageStrings.splice(c, 1);

    var outputArray = [];

    switch(format)
    {
        case 'UriSafe':
            for(var i = 0; i < pageStrings.length; i++)
                pageStrings[i] = uriSafe(pageStrings[i]);
            break;
        case 'Both':
            for(var x = 0; x < pageStrings.length; x++)
            {
                pageStrings[x] = {
                    default: pageStrings[x],
                    safe: uriSafe(pageStrings[x])
                };
            }
            break;
        default:
    }

    function uriSafe(str)
    {
        return str.replace(' ', '').toLowerCase();
    }

    console.log(pageStrings);

    return pageStrings;

}
Dom Ramirez
  • 2,132
  • 2
  • 23
  • 29

3 Answers3

7
var pageStrings = pages;

is creating a reference to the very same array object. When you access it via pageString, you alter the same object which pages does refer to. To create a copy of it (from which you then can splice, assign properties to, etc without altering pages), use

var pageStrings = pages.slice();
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • This is bizarre. I really thought variables were supposed to pass a value. Is this just an array thing or am I way off base? – Dom Ramirez Aug 08 '13 at 21:18
  • 3
    The value here is the reference to the place in memory where the object is stored. Only primitive values like numbers, booleans or strings (which are immutable) do get copied every time. – Bergi Aug 08 '13 at 21:20
  • Oh... That's where my confusion is coming from. Thanks a billion, Bergi! – Dom Ramirez Aug 08 '13 at 21:23
  • It's an array/object thing. Bergi is spot on with his explanation. It will really stand out when your dealing with prototypal inheritance. – ericjbasti Aug 08 '13 at 21:30
1

I think your confusion is around the following line

var pageStrings = pages;

This does not create a copy of pages it simply creates a reference to pages. This means any edit you make to the value of pageStrings (clearing, changing elements, etc ...) will show up on pages because they refer to the same variable.

If you want pageStrings to have a copy of the pages array then do the following

var pageStrings = pages.slice(0);
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

var pageStrings = pages; is your hangup. Keep in mind that when you use = in this way, your new var will be a reference if the argument on the right is and array, object, or function. With strings and numbers you will get the copy you were expecting.

felizuno
  • 23
  • 1
  • 6