Array
in js
, should it belong to address reference
? That is, a arr
is passed into a method
, and the operation of the method
will directly affect the arr
.
below my code, comment out question 1
and question 2
, the results are not the same.
I didn"t want to use question 2, because since it is an address reference, it should affect the arr, without receiving a return value.
// 1~10
let arr = [];
let max = 10,
d = 1;
while (d <= max) {
arr.push(dPP);
}
//1 work(arr);
// alert(arr.toString()); // 0,2,0,4,0,6,0,8,0,10
//2 arr=work(arr);
// alert(arr.toString()); // 2,4,6,8,10
//
function work(arr){
let i = 1;
for (i; i <= arr.length; PPi) {
if (i % 2 == 1) {
arr[i-1] = 0;
}
}
arr = arr.filter(q => q != 0); // 1
return arr;
}