js常用方法学习
array manipulation! The basics.
arr.forEach(callback[, thisArg]) [Array.prototype.forEach()]
说明:为数组中的每个元素执行指定操作
注意:
1. There is no way to stop or break a forEach() loop.
2. it always returns the value undefined.
function logArrayElem(elem, index, arr) { console.log(‘arr[‘ + index + ‘]:::‘ + elem); } [2, 5, 9].forEach(logArrayElem); /* output: arr[0]:::2 arr[1]:::5 arr[2]:::9 */
the use of the ‘thisArg‘ argument。该参数指定后可对其引用 this 关键字
var obj = { showResult: function(value, index) { var squared = this.calcSquare(value); console.log(‘numbers[‘ + index + ‘] = ‘ + value); }, calcSquare: function(x) { return x * x; } } var numbers = [2, 5, 9, 89, 12]; numbers.forEach(function(value, index) { this.showResult(value, index) }, obj); /* output: numbers[0] = 2 numbers[1] = 5 numbers[2] = 9 numbers[3] = 89 numbers[4] = 12 */
arr.slice([begin[, end]]) [Array.prototype.slice()]
说明:returns a shallow copy of a portion of an array into a new array object
var myHonda = { color: ‘red‘, wheels: 4, engine: { cylinders: 4, size: 2.2 } }; var myCar = [myHonda, 2, ‘cherry condition‘, ‘purchased 1997‘]; var newCar = myCar.slice(0, 2); console.log(‘myCar = ‘ + myCar); console.log(‘newCar = ‘ + newCar); console.log(‘myCar[0].color = ‘ + myCar[0].engine.size); console.log(‘newCar[0].color = ‘ + newCar[0].engine.size); /* output: myCar = [object Object],2,cherry condition,purchased 1997 newCar = [object Object],2 myCar[0].color = red newCar[0].color = 2.2 */
slice method can also be called to convert Array-like objects / collections to a new Array.
function list() { return Array.prototype.slice.call(arguments, 1, 3); } var list1 = list(1,2,3,4,5,6); console.log(‘list1 ==> ‘ + list1); /* output: list1 ==> 2,3 */
arr.splice([start, deletecount[, item[, item[, ...]]]]) [Array.prototype.splice()]
说明:changes the content of an array by removing existing elements and/or adding new elements.
var myFish = [‘angel‘, ‘clown‘, ‘mandarin‘, ‘surgeon‘]; // removes 0 elements from index 2, and inserts ‘drum‘ var removed = myFish.splice(2, 0, ‘drum‘); // myFish is [‘angel‘, ‘clown‘, ‘drum‘, ‘mandarin‘, ‘surgeon‘] // removed is [], no elements removed // removes 1 element from index 3 removed = myFish.splice(3, 1); // myFish is [‘angel‘, ‘clown‘, ‘drum‘, ‘surgeon‘] // removed is [‘mandarin‘] // removes 1 element from index 2, and inserts ‘trumpet‘ removed = myFish.splice(2, 1, ‘trumpet‘); // myFish is [‘angel‘, ‘clown‘, ‘trumpet‘, ‘surgeon‘] // removed is [‘drum‘] // removes 2 elements from index 0, and inserts ‘parrot‘, ‘anemone‘ and ‘blue‘ removed = myFish.splice(0, 2, ‘parrot‘, ‘anemone‘, ‘blue‘); // myFish is [‘parrot‘, ‘anemone‘, ‘blue‘, ‘trumpet‘, ‘surgeon‘] // removed is [‘angel‘, ‘clown‘] // removes 2 elements from index 3 removed = myFish.splice(3, Number.MAX_VALUE); // myFish is [‘parrot‘, ‘anemone‘, ‘blue‘] // removed is [‘trumpet‘, ‘surgeon‘]
- push() / pop() — add/remove elements from the end of the array
- unshift() / shift() — add/remove elements from the beginning of the array
arr.join([separator = ‘,‘]) [Array.prototype.join()]
说明:joins all elements of an array into a string.
var a = [‘Wind‘, ‘Rain‘, ‘Fire‘]; var myVar1 = a.join(); var myVar2 = a.join(separator = ‘, ‘); var myVar3 = a.join(separator = ‘ + ‘); var myVar4 = a.join(‘‘); console.log(‘myVar1 ==> ‘ + myVar1); console.log(‘myVar2 ==> ‘ + myVar2); console.log(‘myVar3 ==> ‘ + myVar3); console.log(‘myVar4 ==> ‘ + myVar4); /* myVar1 ==> Wind,Rain,Fire myVar2 ==> Wind, Rain, Fire myVar3 ==> Wind + Rain + Fire myVar4 ==> WindRainFire */
var new_array = arr.concat(value1[, value2[,...]]) [Array.prototype.concat()]
说明:Arrays and/or values to concatenate into a new array
var alpha = [‘a‘, ‘b‘, ‘c‘], numeric = [1, 2, 3]; var alphaNumeric = alpha.concat(numeric); console.log(‘alphaNumeric ==> ‘ + alphaNumeric); var alphaNumeric = alpha.concat(2, 3, [‘你‘, ‘好‘]); console.log(‘alphaNumeric ==> ‘ + alphaNumeric); /* output: alphaNumeric ==> a,b,c,1,2,3 alphaNumeric ==> a,b,c,2,3,你,好 */
var num1 = [1, 2, 3], num2 = [4, 5, 6], num3 = [7, 8, 9]; var nums = num1.concat(num2, num3); console.log(nums); // Result: [1, 2, 3, 4, 5, 6, 7, 8, 9]
concatenates three arrays ↑
arr.unshift([elem1[, ...[, elemN]]])
说明:add the elements to the front of the array.
var arr = [1, 2]; arr.unshift(0); // result of call is 3, the new array length // arr is [0, 1, 2] arr.unshift(-2, -1); // = 5 // arr is [-2, -1, 0, 1, 2] arr.unshift([-3]); // arr is [[-3], -2, -1, 0, 1, 2]
arr.shift()
removes the first element from an array and returns that element
arr.push(elem1, elem2, elem3)
The elements to add to the end of the array.
var sports = [‘soccer‘, ‘baseball‘]; var total = sports.push(‘football‘, ‘swimming‘); console.log(sports); // [‘soccer‘, ‘baseball‘, ‘football‘, ‘swimming‘] console.log(total); // 4
var vegetables = [‘parsnip‘, ‘potato‘]; var moreVegs = [‘celery‘, ‘beetroot‘]; // Equivalent to vegetables.push(‘celery‘, ‘beetroot‘); Array.prototype.push.apply(vegetables, moreVegs); console.log(vegetables); // [‘parsnip‘, ‘potato‘, ‘celery‘, ‘beetroot‘]
Merge the second array into the first one ↑
arr.pop()
说明:removes the last element from an array and returns that value to the caller
var myFish = [‘angel‘, ‘clown‘, ‘mandarin‘, ‘sturgeon‘]; console.log(myFish); // [‘angel‘, ‘clown‘, ‘mandarin‘, ‘sturgeon‘] var popped = myFish.pop(); console.log(myFish); // [‘angel‘, ‘clown‘, ‘mandarin‘ ] console.log(popped); // ‘sturgeon‘
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。