类数组转化为数组
    Array.from()
    every()
    判断数组中的元素是否都符合条件,数组中有个一个不符合条件就返回false
    every(function,this指向)
    返回值:true/false

    1. var personArr = [
    2. { name: '12', src: '../12/123', des: '1221', sex: 'm', age: 10 },
    3. { name: '13', src: '../13/1234', des: '1222', sex: 'f', age: 15 },
    4. { name: '14', src: '../14/1234', des: '1223', sex: 'm', age: 23 },
    5. { name: '15', src: '../15/1245', des: '12214', sex: 'f', age: 14 },
    6. ];
    7. var newPersonArr = personArr.every(function (ele, index, self) {
    8. if (ele.age > 15) {
    9. return false;
    10. }
    11. }, personArr)
    12. console.log(newPersonArr)

    every()原理

    1. var personArr = [
    2. { name: '12', src: '../12/123', des: '1221', sex: 'm', age: 10 },
    3. { name: '13', src: '../13/1234', des: '1222', sex: 'f', age: 15 },
    4. { name: '14', src: '../14/1234', des: '1223', sex: 'm', age: 23 },
    5. { name: '15', src: '../15/1245', des: '12214', sex: 'f', age: 14 },
    6. ];
    7. Array.prototype.myEvery = function(fun){
    8. var _this = arguments[1] || window;
    9. var len = this.length;
    10. var bool = true;
    11. for(var i = 0 ; i < len ; i++){
    12. if(fun.apply(_this,[this[i],i,this]) == false){
    13. bool = false;
    14. break;
    15. }
    16. }
    17. return bool
    18. }
    19. var newPersonArr = personArr.myEvery(function(ele,index,self){
    20. if(ele.age > 15){
    21. return false;
    22. }
    23. })

    some()
    与every()不同every 更像是逻辑运算符&& 而some则是逻辑运算符中的 || 当全为false时才会返回false,当有个一个满足条件时那就为true
    some(function,this指向)
    返回值:true/false

    1. var personArr = [
    2. { name: '12', src: '../12/123', des: '1221', sex: 'm', age: 10 },
    3. { name: '13', src: '../13/1234', des: '1222', sex: 'f', age: 15 },
    4. { name: '14', src: '../14/1234', des: '1223', sex: 'm', age: 23 },
    5. { name: '15', src: '../15/1245', des: '12214', sex: 'f', age: 14 },
    6. ];
    7. var newPersonArr = personArr.some(function (ele, index, self) {
    8. if (ele.age < 11) {
    9. return true;
    10. }else{
    11. return false;
    12. }
    13. }, personArr)
    14. console.log(newPersonArr)

    some() 原理

    1. var personArr = [
    2. { name: '12', src: '../12/123', des: '1221', sex: 'm', age: 10 },
    3. { name: '13', src: '../13/1234', des: '1222', sex: 'f', age: 15 },
    4. { name: '14', src: '../14/1234', des: '1223', sex: 'm', age: 23 },
    5. { name: '15', src: '../15/1245', des: '12214', sex: 'f', age: 14 },
    6. ];
    7. Array.prototype.mySome = function (fun) {
    8. var _this = arguments[1] || window;
    9. var len = this.length;
    10. var bool = true;
    11. for (var i = 0; i < len; i++) {
    12. if (fun.apply(_this, [this[i], i, this]) == false) {
    13. bool = false;
    14. }else{
    15. bool = true;
    16. }
    17. }
    18. return bool
    19. }
    20. var newPersonArr = personArr.mySome(function (ele, index, self) {
    21. if (ele.age < 15) {
    22. return true;
    23. }else{
    24. return false;
    25. }
    26. })

    indexOf()
    方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1

    1. const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
    2. console.log(beasts.indexOf('bison'));
    3. // expected output: 1

    split() 与 join()
    split()按照指定的格式将string拆分为数组

    1. var lrc = `[00:01.06]难念的经
    2. [00:03.95]演唱:周华健
    3. [00:06.78]
    4. [00:30.96]笑你我枉花光心计
    5. `;
    6. var parts = lrc.split("\n");
    7. // 结果为 parts["[00:01.06]难念的经","[00:03.95]演唱:周华健",“[00:06.78]”,“[00:30.96]笑你我枉花光心计”]

    join()将数组按照自定义的格式拆分为string

    1. var arr = [1,2,3,4,5,7];
    2. var a = arr.join('-'); // a = "1-2-3-4-5-7"

    concat()
    拼接两个数组

    1. var arr = [1,2,3,4,5,7]
    2. var arrs = [1,2,3,4,5,6,7]
    3. arr.concat(arrs);

    push()与unshift() , pop() 与 shift()
    push()方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度

    1. var arr = [];
    2. arr.push("123");

    unshift() 方法将一个或多个元素添加到数组的最前面,并返回该数组的新长度

    1. var arr = [1,2,3,4,5,6,7]
    2. arr.unshift(-1,0);

    pop()将从数组最后一位剪贴出来且不接受参数

    1. var arr = [1,2,3,4,5,6,7]
    2. var arrs = [1,2,3,4,5,6,7]
    3. var a = arr.pop(); //将数组最后一位剪贴出来
    4. console.log(a);
    5. arrs.pop(); // 将数组最后一位剪贴出来且不保存
    6. console.log(arrs);

    shift()将从数组第一位剪贴出来且不接受参数

    1. var arr = [1,2,3,4,5,6,7]
    2. var arrs = [1,2,3,4,5,6,7];
    3. var a = arrs.shift(2);
    4. var b = arr.shift();
    5. console.log(a);
    6. console.log(b);

    reverse()
    将数组内的顺序颠倒

    1. var arr = [1,2,3,4,5,6,7]
    2. arr.reverse(); //结果为 arr = [7, 6, 5, 4, 3, 2, 1];

    splice()
    splice(截取的位置,截取多少位,在其切口处添加数据/无限制的添加)

    1. var arr = [1,2,3,4,5,7]
    2. arr.splice(5,0,6); // arr = [1, 2, 3, 4, 5, 6, 7];

    sort()
    给数组排序,默认是以字符编码的顺序进行排序,也可以自定义
    规则为:必须有两个参数
    若 a 小于 b,在排序后的数组中 a 应该出现在 b 之前,则返回一个小于 0 的值。//通俗点讲就是,当返回值为负数时,前面的数在前面
    若 a 等于 b,则返回 0。// 若返回值为0 ,不变
    若 a 大于 b,则返回一个大于 0 的值 //返回值大于零 , 后面的数在前面

    1. //自定义排序
    2. var arr = [2,1,3];
    3. arr.sort(function(a , b){
    4. return a-b;
    5. });
    6. //结果为 arr = [1, 2, 3];