1. Find out the position of element item in a given array arr
function indexOf(arr, item) { //First determine whether the current browser supports indexOf method if (Array.prototype.indexOf){ return arr.indexOf(item); } else { for (var i = 0; i < arr.length; i++){ if (arr[i] === item){ return i; } } } return -1; //Always leak Return-1 outermost }
2. Calculate the sum of all elements in a given array arr
2.1 Player Version
function sum(arr) { let res = 0; for(let i=0;i<arr.length;i++){ res += arr[i]; } return res; }
2.2 version for Each
function sum(arr) { let res = 0; arr.forEach(function (curr,index,arr) { res += curr; }); return res; } console.log(sum([1, 2, 3, 4, 5])); //15
Note: forEach() performs a callback function for each array element; unlike map() or reduce(), it always returns undefined values and is not chained.
function sum(arr) { let res = 0; arr.forEach(function (curr,index,arr) { return res += curr; }); } console.log(sum([1, 2, 3, 4, 5])); //Return undefined
2.3 ES6 Player Edition
function sum(arr) { return arr.reduce((prev,curr) => prev + curr,0) } //Debugging part console.log(sum([1, 2, 3, 4])); //10
3. Remove all elements in the array arr whose values are equal to item. Do not modify arr directly, the result returns a new array
3.1 Edition of Vegetable Chicken
If elements in an array are not equal to item s, add array elements to a new array
function remove(arr, item) { let res = []; for(let i=0;i<arr.length;i++){ if(arr[i] !== item){ res.push(arr[i]); } } return res; }
Another Implementation of 3.2 Vegetable Chicken Version
If the elements in the array are equal to item, the code is no longer executed down.
function remove(arr, item) { let res = []; for(let i=0;i<arr.length;i++){ if(arr[i] === item) continue; res.push(arr[i]); } return res; }
3.3 slice & splice
- The meaning of the question requires not to modify arr directly, and the result returns a new array; slice() method can be used to intercept strings, and return the intercepted content (new array), we can pass parameter 0 to intercept all elements.
- You can't copy an array directly by assignment, which results in changing the elements of an array and the other array as well.
- To operate on a new array, splice() is used to cut the array, and splice() changes the original array.
function remove(arr, item) { let newArr = arr.slice(0); // for(let i=0;i<newArr.length;i++){ if(newArr[i] === item){ newArr.splice(i,1); i--; } } return newArr; }
3.4 ES6 Player Edition (written by others)
The filter() method creates a new array in which the elements are checked for all qualified elements in the specified array. Returns an array of items that return true
function remove(arr,item){ return arr.filter(ele => ele !== item); }
4. Remove all elements of array arr whose values are equal to item, operate directly on a given array of arrs, and return the results.
4.1 If you operate directly on a given array, you can use the splice() method. Note that after the splice() method deletes the elements, the array length also changes, and you need to execute i - to avoid skipping the elements.
function removeWithoutCopy(arr, item) { for(let i=0;i<arr.length;i++){ if (arr[i] === item){ arr.splice(i,1); i--; } } return arr; }
4.2 Delete backwards without considering the length of the array
function removeWithoutCopy(arr, item) { for(let i=arr.length-1;i>=0;i--){ if(arr[i] === item){ arr.splice(i,1); } } return arr; }