0

I have a problem with my javascript, whenever I send an array to another function (through a parameter) to be used and formatted it somehow returns the formatted array back to the previous function.

https://jsfiddle.net/jvrqt0mg/1/

HTML

<p>Column</p>
<select id='colSelect'></select>
<p>Weight</p>
<select id='weightSelect'></select>

JavaScript

updateTableSettings = function () {
var colAmount = "4";
var colArray = [];

//Updates the array with the dropdown options selected by the user
updateDropdown = function (notWeight) {

    //Gets the max amount of weight (The row amount)
    var colWeight = colAmount;
    var colAmount2 = colAmount;

    //Minuses the number selected in the column drop down with weight. 
    colWeight = colWeight - ($("#colSelect option:selected").text() - 1);
    colAmount2 = colAmount2 - ($("#weightSelect option:selected").text() - 1);

    //Checks to make sure that it was the column that changed and not the weight drop down
    colArray[($("#colSelect option:selected").text()) - 1] = (eval($("#weightSelect option:selected").text()) - 1);

    //Resets the weight drop down
    $("#weightSelect").find("Option").remove().end();
    //Repopulates the weight drop down depending on how much weight is left
    for (var i = 0; i < colWeight; i++) {
        //Creates a new option in the weight drop down
        $("#weightSelect").append(new Option(i + 1, (i + 1)));
    }




    //This is me passing the array whenever a drop down has been selected or changed
    //Sends it to the array to the new function(I want dont want the array in function to be formatted)
    format(colArray);



    //This shows the array in this function getting formatted
    console.log(colArray);
    return;
};

//Fills the array       
var fillArray = function () {
    for (var i = 0; i < colAmount; i++) {
        colArray[i] = 0;
    }
};

//Removes all the options to make sure they don't double up
$("#colSelect").find("Option").remove().end();
$("#weightSelect").find("Option").remove().end();

//Populates the array
fillArray();
//Creates the options in the drop down
for (var i = 0; i < colAmount; i++) {
    //Gives the colArray a default of 0 for every column
    $("#weightSelect").append(new Option(i + 1, ("weight" + i)));

    $("#colSelect").append(new Option(i + 1, ("opt" + i)));
}
//Checks column drop down for a change
this.$("#colSelect, #weightSelect").change(function (e) {
    updateDropdown();
});
};



//This is where the array gets passed into
format = function (array) {
//Formats so it pops the array for every value inside the array
formatArray = function (array) {
    var testArray = [];
    testArray = array;
    for (var i = 0; i < array.length; i++) {
        for (var k = 0; k < array[i]; k++) {



            //This seems to delete the array in a different function... HOW?~?!!!
            testArray.pop();



        }
    }
    return;
};
//alert this
formatArray(array);
//I will have code to draw a table row with columns/weight(Size)
};

I want the original array to stay the same so each time a drop down is changed it resends the array.

Sorry for my bad code, I'm new to JS. Thanks in advance

2
  • 3
    please show us only the relevant part of the code, it is hard to look in for a needle in a haystack. Commented Jul 24, 2015 at 8:49
  • I spaced it out so you can see the relevant code, hope that helps Commented Jul 24, 2015 at 8:57

1 Answer 1

1

Before passing array to function make it's copy with slice():

var array = [1, 2, 3, 4];
// makes a copy
var arrayCpy = array.slice(); 
// works with copy and your original array stays intact 
someFunction(arrayCpy); 

That's because arrays get passed by reference, not value. See fiddle: http://jsfiddle.net/2tfeLenL/

Also: http://orizens.com/wp/topics/javascript-arrays-passing-by-reference-or-by-value/

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

1 Comment

Thanks a lot for that, I've been scratching my head for hours on that simple problem

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.