JS Array Object

Array is a Javascript native objects
constructor
var arr = new Array ()
parameter may be a single digital representation to produce an array of length a few, but the values of the respective members are empty, if a plurality of digital words, it means an array member; also It may be a string array and the like. But if the argument is empty, it would read undefined, but not read key name
Example:

var arr = new Array(3);
arr.length // 3
arr // [ empty x 3]

Determining whether an array
returns a boolean Array.isArray (), indicating whether a list, returns a Boolean value

var arr = [1, 2, 3];
Array.isArray(arr) // true

Method
1: valueOf () indicates the object is evaluated, the array itself returns

     var arr = [1, 2, 3];
     arr.valueOf() // [1, 2, 3]

2: toString () Returns a string array, the array becomes string

var arr = [1, 2, 3];
arr.toString() // "1,2,3"

3: push () to add one or more elements of the array at the end, and returns the new added length of the array element, the array will change the original

var arr = [1,2];
console.log(arr.push(3));//3---返回添加新元素后的数组长度

4: () to delete the last element of the array of pop, and returns that element, it will change the original array. Empty array using the pop method returns undefined

var arr = ['a', 'b', 'c'];
console.log(arr.pop()); // 'c'--删除最后一个元素,并将其返回

5: shift () for the first array element deleted, and returns the element may change the original array
The unshift () for adding a first position in the array element, and returns the new length of the array elements, the method changes original array

var a = ['a', 'b', 'c'];
console.log(a.shift()); // 'a'--在第一个位置删除元素a
console.log(a );// ['b', 'c']
a.unshift('d'); // 4--添加d元素,并返回数组长度4

6: join () array into a string. In the designated parameter as a delimiter, to connect all members of the array into a string is returned, or if the array members are null or undefined space, will be converted to an empty string

var a = [1, 2, 3];
console.loga.join(' '));// '1 2 3'
console.loga.join(' | '));// "1 | 2 | 3 "
console.loga.join()); // "1,2,3"

7: concat () array merge, add members to the new array behind the original array members, but does not affect the original array

var a=[1,2,3];
var b=[4,5,6];
console.log(a.concat(b));//[1,2,3,4,5,6]

8: an array for array elements is reversed, to return the changed Reverse (), which will change the original array

var a = ['a', 'b', 'c'];
console.log(a.reverse()); // ["c", "b", "a"]
console.log(a );// ["c", "b", "a"]

9: arr.slice (start, end) Returns a new array, the original array unchanged: two parameters, the first parameter is the initial position, the second parameter as the end position, the left and right opening and closing [start, end) , do not take a small whichever is greater

var a = ['a', 'b', 'c'];
console.log(a.slice(0)) // ["a", "b", "c"]
console.log(a.slice(1, 2)) // ["b"]

10: splice () is used to remove a portion of the original members of the array, and adding a new array member at a position removed, the return value is an element is deleted, if the change is to delete the original array, then the first parameter is the index of the starting position the second parameter is the number of elements to be deleted; insert elements then can add new members directly behind these two elements

var a = ['a', 'b', 'c', 'd', 'e', 'f','g'];
a.splice(4, 2) // ["e", "f"]
a//['a', 'b', 'c', 'd','g'];
a.splice(0, 2, 'h', 'i') // ["a", "b"]
a // [ 'c', 'd','g','h', 'i'];--删除a,b并添加h i

11: sort () array members sorted lexicographically ordered, values are converted to a string, and then proceeds lexicographically sorted, the sorted array former
members is to be changed, a function can be passed as a parameter

实例1: console.log([9,7,8,6].sort());//[6,7,8,9]
 实例2:[3,2,4,1].sort(function (n1,n2){
             return n1-n2;
    });
    console.log(b);//[1,2,3,4]

12: map () will be followed by all members of the array parameter passed, then each time the results of the formation of a new array of return, receive a function, the function passed three parameters, the current members of the current position in the array itself is equivalent to iterate mapping a new array

var numbers = [1, 2, 3];
numbers.map(function (n) {
  return n + 1;
});// [2, 3, 4]
numbers
// [1, 2, 3]

13: forEach traverse the array element, index, array --- current value, the current position, the entire array

var  a=[1,2,3,4];
 a.forEach(function (val, index, arr) {
        arr[index]+=val;
    })
        console.log(a)//[2,4,6,8]

14: filter () array members for filtering member, the composition satisfies the condition returns a new array

 console.log([1,2,3,4].filter(function (val, index, arr) {
       if (val % 2 == 0) {
            return val;
        }
    }));//[2,4]

15: some () returns a Boolean value that indicates whether the array member to determine certain conditions.

var arr = [1, 2, 3, 4, 5];
arr.some(function (elem, index, arr) {
  return elem >= 3;
});
// true---如果有一个大于等于3,就会返回true

16: reduce () // tired multiply accumulate

console.log([1,2,3].reduce(function (totle, current, index) {
        return totle *= current;
    }));//6

Guess you like

Origin blog.51cto.com/14419253/2428254