JavaScript - Array Objects

create an array

  1. Regular way:
	var myCars = new Array()
    myCars[0] = 'Saab'
    myCars[1] = 'Volvo'
    console.log(myCars[1]) // Volvo
  1. Concise way:
	var myCars = new Array('Saab','Volvo','BMW')
    console.log(myCars[1]) // Volvo
  1. Literal expression:
	var myCars = ['Saab','Volvo','BMW']
    console.log(myCars[1]) // Volvo

array property

  1. constructor: Returns the prototype function that creates the array object.
	var myCars = ['Saab','Volvo','BMW']
    console.log(myCars.constructor) // ƒ Array() { [native code] }
  1. The constructor property of the array returns function Array() { [native code] }
  2. Number constructor property returns function Number() { [native code] }
  3. String constructor property returns function String() { [native code] }
  1. length: Returns the number of arrays.
	var myCars = ['Saab','Volvo','BMW']
    console.log(myCars.length) // 3
  1. prototype: Allows adding properties or methods to array objects.
	Array.prototype.myUcase = function() {
    
    
        for(i = 0; i < this.length; i++){
    
    
            this[i] = this[i].toUpperCase()
        }
    }

    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    console.log(fruits) //['Banana', 'Orange', 'Apple', 'Mango']
    fruits.myUcase()
    console.log(fruits) //['BANANA', 'ORANGE', 'APPLE', 'MANGO']

array object methods

  1. concat(): Concatenates two or more arrays. Does not change the existing array, just returns a copy of the concatenated array.
	let mike = ['mike','12']
    let john = ['john','20']
    let hob = ['hob','16']
    let total = mike.concat(john, hob)
    console.log(total) //['mike', '12', 'john', '20', 'hob', '16']
  1. join(): Converts all elements in the array to a string. Elements are separated by the specified delimiter.
	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let energy = classic.join(' & ')
    console.log(energy) // 唐三藏 & 孙悟空 & 猪八戒 & 沙和尚
  1. shift(): removes the first element of the array and returns the removed element. This method changes the length of the array.
	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let remove = classic.shift()
    console.log(remove) // 唐三藏
  1. pop(): removes the last element of the array and returns the removed element. This method changes the length of the array.
	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let remove = classic.pop()
    console.log(remove) // 沙和尚
  1. unshift(): Adds one or more elements to the beginning of the array and returns the new length. This method changes the length of the array.
	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let preAdd = classic.unshift('白龙马','金箍棒')
    console.log(preAdd) //6
    console.log(classic) //['白龙马', '金箍棒', '唐三藏', '孙悟空', '猪八戒', '沙和尚']
  1. push(): Adds one or more elements to the end of the array and returns the new length. This method changes the length of the array.
	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let add = classic.push('白龙马')
    console.log(add) //5
    console.log(classic) //['唐三藏', '孙悟空', '猪八戒', '沙和尚', '白龙马']
  1. splice(): Add or remove elements from an array. Returns an array with elements added or removed. This method mutates the original array.

grammar:array.splice(index,howmany,item1,.....,itemX)

  1. index: Required. Specifies where to add/remove elements. This parameter is the index of the array element to start inserting and/or deleting, and must be a number.
  2. howmany: optional. Specifies how many elements should be removed. Must be a number, but can be "0".
    If this parameter is not specified, all elements from index to the end of the original array are removed.
  3. item1, …, itemX: optional. new element to add to the array

Add a new element to the array:

	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let add = classic.splice(2,0,'白龙马')
    console.log(classic) // ['唐三藏', '孙悟空', '白龙马', '猪八戒', '沙和尚']

Move out the third element of the array and add a new element at the third position of the array:

    let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let change = classic.splice(2,1,'白龙马')
    console.log(classic) // ['唐三藏', '孙悟空', '白龙马', '沙和尚']

Remove two elements after the array starting from the third position:

	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let remove = classic.splice(2,2)
    console.log(classic) //['唐三藏', '孙悟空']
  1. slice(): Returns selected elements from an existing array. Returns the extracted part as a new string. This method does not alter the original array.

Read elements in an array:

	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let newClassic = classic.slice(1,3)
    console.log(newClassic) //['孙悟空', '猪八戒']

Read elements from an array with negative values:

	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let newClassic = classic.slice(-3,-1)
    // let newClassic = classic.slice(-3) //['孙悟空', '猪八戒', '沙和尚']
    console.log(newClassic) //['孙悟空', '猪八戒']

slice(-3,-1):Truncates two elements from the third last (inclusive) to the first last (exclusive).
slice(-3):Truncate the last three elements.

Intercept string:

	let str = 'www.baidu.com!'
    console.log(str.slice(4)) //baidu.com!
    console.log(str.slice(4,9)) //baidu

slice(4):Truncate from the 5th character to the end.
slice(4,10):Truncate from the 5th character to the 9th character.

  1. reverse(): Reverse the order of elements in an array. Returns the array in reversed order.
	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let reverse = classic.reverse()
    console.log(reverse) //['沙和尚', '猪八戒', '孙悟空', '唐三藏']
  1. toString(): Converts an array to a string. and returns the results separated by commas.
	let classic = ['唐三藏','孙悟空','猪八戒','沙和尚']
    let str = classic.toString()
    console.log(str) //唐三藏,孙悟空,猪八戒,沙和尚
  1. sort(): Sorts the elements of an array. The sort order can be alphanumeric and in ascending or descending order. The default sort order is ascending alphabetical order. This method mutates the original array.

Array sorting (alphabetical and ascending):

	let fruits = ["Banana", "Orange", "Apple", "Mango"]
    console.log(fruits.sort()) //['Apple', 'Banana', 'Mango', 'Orange']

Numerical sorting (alphabetical and descending):

    let fruits = ["Banana", "Orange", "Apple", "Mango"]
    fruits.sort()
    console.log(fruits.reverse()) //['Orange', 'Mango', 'Banana', 'Apple']

Numerical sorting (numeric and ascending):

	let points = [40,20,1,5,10,6]
    console.log(points.sort(function(a,b){
    
    return a-b})) //[1, 5, 6, 10, 20, 40]

Descending numbers (numbers and descending):

	let points = [40,20,1,5,10,6]
    console.log(points.sort(function(a,b){
    
    return b-a})) //[40, 20, 10, 6, 5, 1]
  1. filter(): This method creates a new array, and the elements in the new array are checked by checking all the elements in the specified array that meet the conditions. This method does not mutate the original array.
	let ages = [20, 15, 35, 12]
        
    function checkAdult(age){
    
    
        return age >= 18
    }

    let pass = ages.filter(checkAdult)
    console.log(pass) //[20, 35]
  1. map(): This method returns a new array, and the elements in the array are the values ​​processed by the calling function of the original array elements. The map() method processes the elements sequentially in the original array element order. This method does not mutate the original array.

Returns an array whose elements are the square roots of the original array:

	let numbers = [4, 9, 16, 25]

    function Fn(){
    
    
        return numbers.map(Math.sqrt)
    }

    console.log(Fn()) // [2, 3, 4, 5]
  1. forEach(): used to call each element of the array and pass the element to the callback function. forEach() will not execute the callback function for an empty array.

List each element of an array:

	let numbers = [4, 9, 16, 25]

    function Fn(item, index){
    
    
        let result = 'index[' + index + ']:' + item
        console.log(result)
    }

    numbers.forEach(Fn)
    
	/* 输出:
	index[0]: 4
	index[1]: 9
	index[2]: 16
	index[3]: 25 
	*/

Use the return statement to achieve the continue effect:

	let arr = [1,2,3,4,5]
    arr.forEach(function (item){
    
    
        if(item === 3){
    
    
            return
        }
        console.log(item) // 1 2 4 5
    })

Use the return statement to achieve the break effect:

	let arr = [1,2,3,4,5]
       
    arr.every(function (item){
    
    
        console.log(item) // 1 2 3
        return item !==3
    })
  1. keys(): Used to create an iterable object containing the array keys from an array. Returns true if the object is an array, false otherwise.
	let fruits = ["Banana", "Orange", "Apple"];
    let result = fruits.keys()

    console.log(result.next().value) // 0
    console.log(result.next().value) // 1
    console.log(result.next().value) // 2
    console.log(result.next().value) // undefined

不积跬步无以至千里 不积小流无以成江海

Guess you like

Origin blog.csdn.net/qq_45902692/article/details/123485842