(I) array to a string
⑴ String (arr) : The arr each element into a string, separated by commas
Fixed routine: an array of photographs: used to identify whether the array has been modified
⑵ arr.join ( "connector") : The arr each element into a string, separated by a custom connector
// characters into words splicing
var chars = [ "H", "E", "L", "L", "O"];
the console.log (chars.join ( "")); // the Hello
⑶ fixed routine
① characters to form words: chars.join ( "") -> seamless
Extension: Analyzing array is empty array: arr.join ( "") == ""
② the-word sentence: words.join ( "")
③ array into page content elements:
"<Start label>" +
arr.join ( "</ end tag> <tag start>")
+ "</ End tag>"
(Ii) stitching and select
Without directly modifying the original array, and returns a new array!
⑴ splicing
concat () splicing two or more arrays, and returns the result
var newArr = arr1.concat (value 1, value 2, arr2, value 3, ...)
The value 1, value 2 and arr2 each element, and are then spliced to the value 3 arr1 element returns the new array
Where: arr element will be broken first, and then stitching
of arr1 = var [90, 91];
var arr2 is = [80 and 81];
var ARR3 = [70,71,72,73];
var = arr4 arr1.concat (50 and 60, arr2 is, ARR3);
Console .log (arr1); // 90,91,92 conventional array values unchanged
console.log (arr4); // 90,91,92,50,60,80,81,70,71,72,73
⑵ select
slice () Returns a conventional array subarray
var = subArr arr.slice (Start, Andy + 1)
Select arr position in starti start to finish endi all the elements of the new array is returned - the original array remains unchanged
He stressed: Any two parameters are the subject of the next function, there is a feature:
Containing free end of the head
where, R1 = [10,20,30,40,50];
was arr2 arr1.slice = (1,4); // 20,30,40
was arr3 = arr1.slice (2); // 30,40,50
arr.slice arr4 = var (-4, -2); // 20,30
the console.log (of arr1) // 10,20,30,40,50 existing array elements unchanged
⑶ select shorthand
① selection up to the end of: the second parameter may be omitted
② If the selected element from the end of the past: Available reciprocal index:
arr.slice(arr.length-n,arr.length-m+1);
Can be abbreviated as: arr.slice (-n, -m + 1);
③ Copy array:
arr.slice(0,arr.length);
Can be abbreviated as: arr.slice ();
(Iii) modify the array
⑴ delete
splice directly modifying the original array
arr.splice(starti,n);
删除arr中starti位置开始的n个元素不考虑含头不含尾
其实:var deletes = arr.splice(starti,n);
返回值deletes保存了被删除的元素组成的临时数组
var arr1 = [10,20,30,40,50];
var arr2 = arr.splice(2,1);
//var arr2 = arr1.splice(2,2,21,22,23);
//var arr2 = arr1.splice(2,2,[91,92,93]);
console.log(arr1);
conlole.log(arr2);
⑵插入
arr.splice(starti,0,值1,值2,...)
在arr中starti位置,插入新值1,值2,...原starti位置的值及其之后的值被向后顺移
⑶替换
其实就是删除旧的,插入新的
arr.splice(starti,n,值1,值2,...)
先删除arr中starti位置的n个值,再在starti位置插入新值
强调:删除的元素个数和插入的新元素个数不必一致。
㈣颠倒数组
reverse() 颠倒数组中元素的顺序
arr.reverse()
var arr1 = [10,20,30,40,50];
arr1.reverse();
console.log(arr1);
强调:仅负责原样颠倒数组,不负责排序
㈤排序
将元素按从小到大的顺序重新排列
⑴排序API
arr.sort():默认将所有元素转为字符串再排列
问题:只能排列字符串类型的元素
解决:使用自定义比较器函数
⑵排序算法
(手写)冒泡 快速 插入排序