我的 javascript 有问题,每当我将数组发送到另一个函数(通过参数)进行使用和格式化时,它都会以某种方式将格式化的数组返回到前一个函数。
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)
};
我希望原始数组保持不变,因此每次更改下拉列表时都会重新发送数组。
抱歉我的代码不好,我是 JS 新手。 提前致谢
最佳答案
在将数组传递给函数之前,使用 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);
这是因为数组是通过引用传递的,而不是值。参见 fiddle :http://jsfiddle.net/2tfeLenL/
另外:http://orizens.com/wp/topics/javascript-arrays-passing-by-reference-or-by-value/
关于javascript - 变量通过 Javascript 函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606018/