The method of operation of the array js

World let me black and blue, but the wound is grown wings.

Several methods of operation of the array

Several ways to declare an array of

     字面量 声明 
     var arr = [];
     var arr = [12,3,4];
    new 一个数组
    new Array()
  1. Detection is not an array,
    var arr = [12, 3];
    var o = {};
    // instanceof 检测是不是数组
    console.log(arr instanceof Array); // true
    console.log(o instanceof Array); // false
    console.log(o instanceof Object); // true   通俗点 你是不是俺的  前者属不属于后者
    // isArray()  
    console.log(Array.isArray(arr)); // true
    console.log(Array.isArray(o)); // false
  1. end of the array to push one or more additional elements, modify the original returned array length is the total length of the new array
    var arr = ['red', 'green'];
    arr.push('blue', 'pink'); // 给 arr 末尾添加2个 blue pink
    var ar1 = arr.push('blue', 'pink');  // ar1    4
    console.log(arr.push('blue', 'pink')); // push 有返回值 返回的是总的长度 length
    console.log(arr); //  red  green blue 修改 原数组
  1. pop () to remove one end of the array elements (can only delete one) ;; no arguments and returns the removed element
    var arr = ['pink', 'hotpink', 'deeppink'];
    arr.pop();   不需要参数
    console.log(arr.pop()); // deeppink  返回的 被删除的那个元素 值
    console.log(arr); //  ["pink", "hotpink"]
  1. the unshift () to add one or more elements in front of the array, modifying the original array, returns the length of a length
    var arr = ['pink', 'hotpink', 'deeppink'];
    // arr.unshift();
    console.log(arr.unshift('yellow')); //  4
    console.log(arr);  // ["yellow", "pink", "hotpink", "deeppink"]
  1. shift () removes an element in front of the array (only delete a) ;; takes no arguments, returns the removed element
    var arr = ['pink', 'hotpink', 'deeppink'];
    // arr.shift();
    console.log(arr.shift()); //  pink
    console.log(arr); // ["hotpink", "deeppink"]
  1. reverse () flip, modify the original array
    var arr = ['pink', 'red', 'blue'];
    arr.reverse(); // 反转数组 修改原数组 
    console.log(arr);
    返回的结果是 : ["blue", "red", "pink"]
  1. Sort sort ()
    var arr = [2, 5, 9, 1, 4];
    arr.sort(); // 无参数 按照 ascii 排列 
    console.log(arr);
    var arr = [12, 5, 9, 31, 4];
    arr.sort();
    console.log(arr); // 修改原数组
    
    
    var arr = [11, 34, 6, 9, 1];
    // arr.sort(function(){})
    // 公式
    arr.sort(function(a, b) {
       return a - b // 升序排序
    })
    console.log(arr);
    
    var arr = [11, 34, 6, 9, 1];
    // arr.sort(function(){})
    // 公式
    arr.sort(function(a, b) {
         return b - a // 降序排序
    })
    console.log(arr);
  1. concat () array inside the stitching, a plurality of parameters can be
    var arr = ['red'];
    var arr1 = ['andy'];
    // console.log(arr + arr1);
    console.log(arr.concat(arr1, 'pink')); // 两个数组相连 合并成为一个数组
    输出的结果为 : ["red", "andy", "pink"] 拼接之后的数组
    console.log(arr);
    输出的结果为 : ["red"] 由于没有存变量,返回的就是原数组,也说明了,不会修改原数组
  1. splice (,) remove (intercept), beginning from the first of several, a few modified to delete the original array (and POP (); and shift () as) returns the removed element

Here I enclose my QQ: 2489757828 a problem, then you can explore with
my private blog: Lida Xuan

Guess you like

Origin blog.csdn.net/weixin_43553701/article/details/93387163