


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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
