Home > Article > Web Front-end > How to analyze the usage of array types in classic js ability assessment questions_with code
I am currently working on a classic js ability assessment question, so I summarized it according to my own abilities and ideas. This article is mainly a summary of the array class.
1. Find the position of the element item in the given array: arr.indexOf(item);
2. Calculate the sum of all elements in the given array arr: arr .forEach(function(e){sum=sum e;})
Note: Array name.forEach(function(array element, element index, additionally defined array){function body}) / /What is returned is still the called array
Array name.map(function(array element){function body}) //What is returned is a new array, which does not modify the called array;
3. Remove the elements in the array arr whose ownership is equal to item. Do not modify the array arr directly. The result will be a new array.
function remove(arr, item) { var newArray=[]; arr.forEach(function(e){ if(e!=item){ newArray.push(e); } }) return newArray; }
4. Remove all elements in the array arr that are equal to item, directly modify the given array, and return the result.
//方法一:从前往后遍历删除元素 function removeWithoutCopy(arr,item){ for(i=0;i<arr.length;i++){ if(item==arr[i]){ arr.splice(i,1); //splice(定位元素索引[,删除元素个数,添加元素]) i--;//i--的目的是因为删除了一个元素,在回到for时需要i++,会跳过一个元素 } } } //方法二:从后往前遍历删除元素 function removeWithoutCopy(arr,item){ for(i=arr.length-1;i>=0;i--){ if(item==arr[i]){ arr.splice(i,1); //删除时无需进行位移 } } }
Note: Array name.splice (index [, number, add elements]), splice means directly modifying the original array, and the return value is an array composed of deleted elements. Example: var a=[1,2,3,4,5];a.splice(3,1)//Return [4], a is [1,2,3,5]
5 , add the element item at the end of the array arr, do not modify arr directly, and return a new array.
//方法一:直接使用数组的concat追加新的item,并返回新的数组 function append(arr, item) { return arr.concat(item); } //方法二:利用slice对原数组进行切分,产生新的数组,在对新数组进行push即可 function append(arr, item) { var newArray=arr.slice(0); newArray.push(item); return newArray; } //方法三:利用数组的map创建新的数组,最后对新数组push(item)即可 function append(arr, item) { var newArray=arr.map(function(e){ return e; }) newArray.push(item); return newArray; } //方法四:定义一个空数组,利用for/forEach进行赋值,再对新数组push(item)即可 function append(arr, item) { var newArray=[]; arr.forEach(function(e){ newArray.push(e); }) newArray.push(item); return newArray; }
6. Delete the last element of array arr. Do not modify array arr directly. The result will be a new array: return arr.slice(0,arr.length-1);
7. Add element item at the beginning of array arr. Do not modify the array arr directly. The result will be a new array: var newArray=arr.slice(0);newArray.unshift(item)
8. Delete the first element of the array arr. Do not modify the array arr directly. The result is a new array: return arr.slice(1);
9. Merge array arr1 and array arr2. Do not modify the array arr directly. The result will be a new array: return arr1.concat(arr2);
10. Add the element item at the index of the array arr. Do not modify the array arr directly. The result is a new array: var newArray = arr.slice(0); newArray.splice(index,0,item); return newArray;
11. The median value of the statistical array arr is equal to The number of occurrences of item's elements.
function count(arr, item) { var count=0; arr.forEach(function(e){ if(e==item) count+=1; }); return count; }
12. Find the repeated elements in the array arr. Input: [1, 2, 4, 4, 3, 3, 1, 5, 3]. Output: [1, 3, 4].
//方法一:利用indexOf和lastIndexOf进行判断 function duplicates(arr) { var result=[]; arr.forEach(function(e){ if(arr.indexOf(e)!=arr.lastIndexOf(e) && result.indexOf(e)==-1){ result.push(e); } }); return result; } //方法二:利用双重for循环进行判断 function duplicates(arr) { var result=[]; for(i=0;i<arr.length-1;i++){ for(k=0;k<result.length;k++){ if(arr[i]==result[k]) break; } if(k!=result.length) continue; for(j=i+1;j<arr.length;j++){ if(arr[i]==arr[j]){ result.push(arr[i]); break; } } } return result; }
13. Find the second power of each element in the array arr. Do not modify the array arr directly, the result will be a new array.
//方法一:定义空数组,对arr进行遍历赋值 function square(arr) { var newArr=[]; arr.forEach(function(e){ newArr.push(e*e); }); return newArr; } //方法二:直接使用map产生新的数组 function square(arr) { return arr.map(function(e){ return e*e; }) }
14. In the array arr, find all positions where the element whose value is equal to item appears
function findAllOccurrences(arr, target) { var newArray=[]; arr.forEach(function(e,i){ if(e==target) newArray.push(i); }) return newArray; }
In the classic js questions, the above is the question type of all arrays, and then the following is It is a coding specification and function module.
Related recommendations:
BootStrap classic case analysis
PHP string operation classic introduction
Video tutorial: 27 classic practical exercises for front-end JS development
The above is the detailed content of How to analyze the usage of array types in classic js ability assessment questions_with code. For more information, please follow other related articles on the PHP Chinese website!