数组的方法以及特征


 
      数组变成一个字符串:
      1.join() : 可以在每一个元素中加一个字符串
      2.toString()
   

     
      pop(): 删除数组最后一个元素
      push(): 增加数组
      reverse() : 反转
      slice(): 从下标开始取
      sort(): 对数组进行排序,
      splice(): 删除数组的某几个
      toString(): 把数组变成字符串
      unshift() : 新增最前面的
      split(): 用于把一个字符串分割成字符串数组.
      every() : 完全
      some(): 不完全,只要有一个满足就行了
      filter() : 过滤函数
      reduce()
      reduceRight(): 
    
       * 不改变:  1. concat(),是不会改变原数组的
       *          2. join()
       *          3. slice()
       *          4. toString()
       *          5. map()
       *          6. every()
       *          7. some()
       *          8. filter()
       *          9. reduce()
       *          10. reduceRight()
       *
       * 改变:   1.  pop()
       *          2. push()
       *          3. reverse()
       *          4. shift()
       *          5. sort()
       *          6. splice()
       *          7. unshift()
       *
       * 不确定: forEach,map
       *
       */

       var arr1 = [1, 2, 3]
        arr2 = [2, 3, 4]
        arr3 = []
        arr3 = arr1.concat(arr2)
        console.log(arr1)
        console.log(arr2)
        console.log(arr3) */

       var arr1 = [1, 2, 3]
    res = null;
    res = arr1.join(',')
    console.log(res)


    
      var arr1 = [1, 2, 3]
      res = null;
      res = arr1.pop()
      console.log(arr1, res) 
   

     var arr1 = [1, 2, 3]
    res = null;
    res = arr1.push(4)
    console.log(arr1, res) 

    var arr1 = [1, 2, 3]
    res = null;
    res = arr1.reverse()
    console.log(arr1, res) 

       
      var arr1 = [1, 2, 3]
      res = null;
      res = arr1.shift()
      console.log(arr1, res) 
    

      
      var arr1 = [1, 2, 3, 4, 5, 6]
      res = null;
      res = arr1.slice(3)
      console.log(arr1, res) */

        var arr1 = [3, 2, 3, 4, 5, 6]
        res = null;
        res = arr1.sort()
        console.log(arr1, res)
     
        var arr1 = [3, 2, 3, 4, 5, 6]
        res = null;
        res = arr1.splice(4, 2)
        console.log(arr1, res)
    
        var arr1 = [3, 2, 3, 4, 5, 6]
        res = null;
        res = arr1.toString()
        console.log(arr1, res) */
      
      var arr1 = [3, 2, 3, 4, 5, 6];
      res = null;
      res = arr1.unshift(11);
      console.log(arr1, res); */

     
      var arr1 = [3, 2, 3, 4, 5, 6];
      res = null;
      var str = "2,123,456";
      var res = str.split(",");
      console.log(str, res);


      // 注意❤️:用forEach是不会改变数组里面的值的,不会改变原数组的,forEach是不会改变value里面的值的,要改变的话,只能通过数组改变,forEach是不确定的
      /*    var arr1 = [3, 2, 3, 4, 5, 6];
      res = [];
      arr1.forEach(function (value, index) {
        // value += 1;
        arr1[index] += 1;
      });
      console.log(arr1); */

      var arr1 = [3, 2, 3, 4, 5, 6];
      res = [];
      res = arr1.map(function (value, index) {
        return value + 1;
      });
      console.log(arr1, res); 
      var arr1 = [{ value: 1 }, { value: 2 }, { value: 3 }];
      res = [];
      res = arr1.map(function (value, index) {
        return value.value;
      });
      console.log(arr1, res);

      // 引用类型,是指向同一个 地址的
      var arr1 = [{ value: 1 }, { value: 2 }, { value: 3 }];
      res = [];
      res = arr1.map(function (value, index) {
        value.name = index;
        return value;
      });
      console.log(arr1, res);
 
      var arr1 = [{ value: 1 }, { value: 2 }, { value: 3 }];
      res = [];
      res = arr1.map(function (value, index, array) {
        console.log(value);
        console.log(index);
        console.log(array);
        return value, index, array;
      });
      console.log(arr1, res); */

      
        map,forEach的区别:
          1.forEach是不确定要不要改变原数组的
          2.map是需要改变原数组的 ,(return出来新的值,return过后是新的数组了)
          3.map是映射函数,把一个数组关联到另一个数组,,,(后端mapping,映射关系,表的关联模型)
          4.map是数组扩展的方法
      
      var arr = [1, 2, 3, 4, 5, -6, 7];
      res = [];
      res = arr.every(function (elem) {
        return elem > 0;
      });
      console.log(arr, res);

      var arr = [1, 2, 3, 4, 5, 6, 7];
      res = [];
      res = arr.some(function (elem) {
        return elem < 0;
      });
      console.log(arr, res);

      var arr = [1, 2, 3, 4, 5, -6, 7];
      res = [];
      res = arr.filter(function (elem) {
        return elem > 0;
      });
      console.log(arr, res);

      /*  var arr = [1, 2, 3, 4, 5, -6, 7];
      res = [];
      res = arr.reduce(function (prev, value) {
        prev.push(value + 1);
        return prev;
      }, res);
      console.log(arr, res);

      var arr = [1, 2, 3, 4, 5, -6, 7];
      res = [];
      res = arr.reduceRight(function (prev, value) {
        prev.push(value + 1);
        return prev;
      }, res);
      console.log(arr, res);

 

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页