Array常用方法总结

来源:互联网 发布:centos 编译安装lnmp 编辑:程序博客网 时间:2024/04/27 02:42

转换方法

  • toLocaleString(): toString(): valueOf():

  • push():栈中项插入 pop():栈中项移除

队列

  • push():向数组末端添加项 shift():从数组前段取得项
var color =['orange','yellow','green'];     color.push('red');     // color.pop();     // color.shift();     // color.reverse();     // color.sort();     // var color2 = color.slice(1,3); //length = 3-1,index =  0,1,2,3->[1,3)         var color2 = color.splice(1,2); //length = 3-1,index =  0,1,2,3->[1,3)    // alert(color2 );    // console.log(color);    // console.log(color2);

重排序方法

  • reverse()和sort()方法。

操作方法

  • concat()返回一个数组 slice()返回一个数组

splice()
- 删除 插入 替换 返回一个数组

位置方法

  • indexOf()和lastIndexOf()

迭代方法

  • every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true。
    some():对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true。
    filter():对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组。
    forEach():对数组中的每一项运行给定函数。这个方法没有返回值。
    map():对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组

归并方法

  • reduce()和reduceRight()//reduce()从前到后,reduceRight()从后到前
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];    var res     res= numbers.every(function(item, index, array){        // console.log(item, index, array);         return (item >=1);    });     // console.log(res);      res= numbers.filter(function(item, index, array){        // console.log(item, index, array);         return (item > 2);    });      // console.log(res);        res= numbers.some(function(item, index, array){        // console.log(item, index, array);         return (item > 2);    });      // console.log(res);       res= numbers.map(function(item, index, array){        // console.log(item, index, array);         return (item += 2);    });      //  console.log(numbers);      // console.log(res);     numbers.forEach(function(item,index, array){        // console.log(item);        // console.log(array[index]);        // console.log(array);    });     var sum = numbers.reduce(function(prev, cur, index, array){        // console.log(prev, cur, index, array);        return prev+=cur;     });     console.log(sum);
原创粉丝点击