map method will loop through the array of arrays, each array to be processed in a function, and returns a function after treatment; but if we change the array itself in a function, then what will happen?
var removeElement = function(nums, val) { nums.map(function (item, index){ if(item === val){ console.log(index); // 2 3 nums.splice(index, 1); } }); }; removeElement([1,2,3,4,3,5], 3);
As can be seen by the above example, if we change the original array in one cycle of a case, then the next processing cycle is carried out on the basis of the array has changed.
var removeElement = function (the nums, Val) { nums.map ( function (Item, index) { IF (Item === Val) { nums.splice (index, . 1); // deleted index entry array 2 , and after a 2 becomes item 2 of the index, but the index has been treated 2, so 2 this index is no treatment, leading to a 2 is not removed. } }); }; removeElement ([ 0, 1,2,2,3,0,4,2], 2);
If the above code is an array: [0,1,2,2,2,3,0,4,2], nums [0, 1, 2, 3, 0, 4]; if [0, 1,2,2,2,2,3,0,4,2], nums [0, 1, 2, 2, 3, 0, 4]