code
Method 1: return a new array
/ / remove all elements in array arr whose values are equal to item. Do not modify the array arr directly, and the result will return a new array
instead**
function clearArrItem(arr,item){
var arrs=[];
for(var i = 0;i<arr.length;i++){
if(arr[i] !== item){
arrs.push(arr[i])
}
}
return arrs
}
var arrs = [1,2,5,4,2,1,5,2,1,2,5,42,1,4,11,1,1,1,1,11];
console.log(clearArrItem(arrs,1));
**
Method 2: operate in the original array
/ / remove all the elements in the array arr whose values are equal to the item, directly operate on the given arr array, and return the result
to the user**
function clearArrItem2(arr,item){
var index;
for(var i = 0;i<arr.length;i++){
if(arr[i] === item){
if(arr[i+1] === item){
arr.splice(i,1);
i--;
continue;
}
arr.splice(i,1);
}
}
return arr;
}
var arrs = [1,5,6,3,5,4,1,1,1,1,1,1,1,5,8,4,5,1,5,1,5,1,1];
console.log(clearArrItem2(arrs,1));
**
Read More:
- Array of PHP_ diff,array_ intersect,array_ merge, in_ Is there a limit on the number of arrays in array?
- If JavaScript exceeds the length of the array, no error will be reported
- [solved] error: valueerror: expected 2D array, got scalar array instead
- Git removes the content of stash
- Shell removes double quotes from strings
- Two dimensional array and pointer to one dimensional array
- JavaScript realizes the longest substring of non repeated characters
- socket.error : [errno 10048] and view PID process number and port number
- Leetcode 34 finds the first and last position of an element in the sorted array (medium)
- How to use JavaScript in HTML
- JavaScript solves let statement error in code
- Variable type error: error of array occurs when TP5 controller receives the array of post
- In pandas, dataframe and np.array The mutual transformation between the two
- A JavaScript error occurred in the main process
- Error in comparing the size function of STL with negative number
- PHP & nbsp; built in server array
- A JavaScript error occured in the main process
- Solution to null pointer error in array
- Error in pom.xml dependency version number in idea
- [Solved] CAP_IMAGES: can‘t find starting number (in the name of file)