Home > Web Front-end > JS Tutorial > How to analyze the usage of array types in classic js ability assessment questions_with code

How to analyze the usage of array types in classic js ability assessment questions_with code

php是最好的语言
Release: 2018-07-26 11:39:45
Original
1077 people have browsed it

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;
}
Copy after login

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); //删除时无需进行位移
        }
    }
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
    })
}
Copy after login

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template