Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES5-数组方法 #12

Open
hubvue opened this issue Dec 19, 2018 · 0 comments
Open

ES5-数组方法 #12

hubvue opened this issue Dec 19, 2018 · 0 comments

Comments

@hubvue
Copy link
Owner

hubvue commented Dec 19, 2018

Array.prototype.indexOf

查找一个值在不在数组中,从前往后查找,在的话返回值的索引,不在的话返回-1

Array.prototype.lastIndexOf

查找一个值在不在数组中,从后往前查找,在的话返回值的索引,不在的话返回-1

Array.prototype.every

查找数组中每一个元素,直到有一个不符合条件,返回false为止。否则返回true

 var arr = [2,2,2,2].every(function(item,index,arr){
    return item == 2;
})
console.log(arr);

Array.prototype.some

找到数组中第一个符合要求的值后就不再继续执行。找到返回true,未找到返回false

var arr = [1,2,3,2].some(function(item,index,arr){
    console.log(item);      //1,2
    return item == 2;
})
console.log(arr);   //true

Array.prototype.forEach

循环数组,对数组进行操作

 [1,2,3,4].forEach(function(item,index,arr){
    console.log(item);
})

Array.prototype.map

map处理数组中的所有值并返回处理后的值,不影响原数组,返回结果为新的数组。

 var arr = [1,2,3,4].map(function(item,index,arr){
    return item * item;
})
console.log(arr);

Array.prototype.filter

filter是对数组元素的过滤,把返回true的汇集成新的数组,返回结果为新的数组。

var arr = [1,2,3,4].filter(function(item,index,arr){
    return item ==2;
})
console.log(arr);

Array.prototype.reduce

使用指定的函数将数组元素进行整合,生成单个值。这是在函数式编程中是常见的操作,也可以称为‘注入’和‘折叠’

var arr = [1,2,3,4].reduce(function(a,b){
    return a + b;
})
console.log(arr);   //10 

Array.prototype.reduceRight

和上面方法一样,是从后往前整合

var arr = [1,2,3,4].reduceRight(function(a,b){
    return a + b;
})
console.log(arr);   //10 

Array.isArray

判断一个对象是不是数组。

var arr = [];
var a;
console.log(Array.isArray(arr));        //true
console.log(Array.isArray(a))       //false

TypedArray

TypedArray 是一种通用的固定长度缓冲区类型,允许读取缓冲区中的二进制数据。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant