js notes -0

#js notes -0

Array:

  • indexOf method:
    • Array also can search a specified element by indexOf () Location:

        var arr = [10, 20, '30', 'xyz'];
        arr.indexOf(10); // 元素10的索引为0
        arr.indexOf(20); // 元素20的索引为1
        arr.indexOf(30); // 元素30没有找到,返回-1
        arr.indexOf('30'); // 元素'30'的索引为2
  • slice
    • slice () is corresponding to a String substring () version, that it intercepts part of the element in the Array, and then returns a new Array:

        var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
        arr.slice(0, 3); // 从索引0开始,到索引3结束,但不包括索引3: ['A', 'B', 'C']
        arr.slice(3); // 从索引3开始到结束: ['D', 'E', 'F', 'G']

    He noted that the slice () start and end parameters include start index, not including the end of the index.

    If not to slice () pass any parameters, it will start to finish all the elements taken. Using this, we can easily copy an Array:

      var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
      var aCopy = arr.slice();
      aCopy; // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
      aCopy === arr; // false
  • pop and push
    • push () Array add several elements to the end of the last element pop () Array's put deleted:

        var arr = [1, 2];
        arr.push('A', 'B'); // 返回Array新的长度: 4
        arr; // [1, 2, 'A', 'B']
        arr.pop(); // pop()返回'B'
        arr; // [1, 2, 'A']
        arr.pop(); arr.pop(); arr.pop(); // 连续pop 3次
        arr; // []
        arr.pop(); // 空数组继续pop不会报错,而是返回undefined
        arr; // []
  • unshift和shift

    If several elements to be added to the head Array using the unshift () method of the first element, Shift () method put Array deleted:

      var arr = [1, 2];
      arr.unshift('A', 'B'); // 返回Array新的长度: 4
      arr; // ['A', 'B', 1, 2]
      arr.shift(); // 'A'
      arr; // ['B', 1, 2]
      arr.shift(); arr.shift(); arr.shift(); // 连续shift 3次
      arr; // []
      arr.shift(); // 空数组继续shift不会报错,而是返回undefined
      arr; // []
  • sort
    when the sort () to sort the current Array, it will directly modify the position of the current element of Array, direct call, according to the default sort order:

      var arr = ['B', 'C', 'A'];
      arr.sort();
      arr; // ['A', 'B', 'C']
  • Reverse
    Reverse () Array element to the whole out all of them, which is the reverse:

      var arr = ['one', 'two', 'three'];
      arr.reverse(); 
      arr; // ['three', 'two', 'one']
  • splice
    splice () method is to modify the Array "universal method", it can start to delete some elements from the specified index, and then add several elements from this location:

      var arr = ['Microsoft', 'Apple', 'Yahoo', 'AOL', 'Excite', 'Oracle'];
      // 从索引2开始删除3个元素,然后再添加两个元素:
      arr.splice(2, 3, 'Google', 'Facebook'); // 返回删除的元素 ['Yahoo', 'AOL', 'Excite']
      arr; // ['Microsoft', 'Apple', 'Google', 'Facebook', 'Oracle']
      // 只删除,不添加:
      arr.splice(2, 2); // ['Google', 'Facebook']
      arr; // ['Microsoft', 'Apple', 'Oracle']
      // 只添加,不删除:
      arr.splice(2, 0, 'Google', 'Facebook'); // 返回[],因为没有删除任何元素
      arr; // ['Microsoft', 'Apple', 'Google', 'Facebook', 'Oracle']
  • concat
    • concat () method of the current Array Array and the other connected, and returns a new Array:

        var arr = ['A', 'B', 'C'];
        var added = arr.concat([1, 2, 3]);
        added; // ['A', 'B', 'C', 1, 2, 3]
        arr; // ['A', 'B', 'C']

Please note, concat () method does not modify the current Array, but returns a new Array.

    实际上,concat()方法可以接收任意个元素和Array,并且自动把Array拆开,然后全部添加到新的Array里:
    
    var arr = ['A', 'B', 'C'];
    arr.concat(1, 2, [3, 4]); // ['A', 'B', 'C', 1, 2, 3, 4]
  • join
    • join () method is a very practical way, each element of the current Array which are connected with the specified string, and then returns the concatenated string:

            var arr = ['A', 'B', 'C', 1, 2, 3];
            arr.join('-'); // 'A-B-C-1-2-3'

      Array elements if not a string, then the connection is automatically converted to a string.

Guess you like

Origin www.cnblogs.com/chenyameng/p/11443605.html